Skip to content

Conversation

@tlsproxy
Copy link

Hello, I encountered a problem when using this tool. I am unable to obtain field contents across levels to create a new structure. For example, integration of fields across levels, as follows:

copy

Copy the values into the target fields.

copy(from, to, key?: string)

Examples:

const data = [
  {
    "date": "2025/1/19",
    "location": [
      {
        "x": 2.7391487568478716,
        "y": 4.424778761061947
      }
    ]
  }
]

jsonquery(data, `copy(.date,.location,"key")`)
// [
//     {
//         "date": "2025/1/19",
//         "location": [
//             {
//                 "x": 2.7391487568478716,
//                 "y": 4.424778761061947,
//                 "key": "2025/1/19"
//             }
//         ]
//     }
// ]

I forked this repository and added a copy function to solve my problem. I am not very good at writing TypeScript code, but I have still completed the writing and testing. Now I am initiating a PR. If you think it is necessary, you can merge it.
Thank you for your open source. Best regards.

@tlsproxy tlsproxy closed this Feb 6, 2025
@josdejong
Copy link
Collaborator

@tlsproxy I see you closed this PR. I haven't yet had a look at it though. What is the reaason for closing your PR?

@tlsproxy
Copy link
Author

tlsproxy commented Feb 6, 2025

@tlsproxy I see you closed this PR. I haven't yet had a look at it though. What is the reaason for closing your PR?

Later, I reflected on it. I'm not quite sure if this is a universal requirement. Since your library also provides the ability to define custom functions, I closed it. My actual scenario is to convert the data structure exported from the data annotation platform into the data structure required for model fine - tuning. I need to flatten the structure fields (integrate multi - level fields). Now I've reopened it. You can decide whether to close or merge it.

@tlsproxy tlsproxy reopened this Feb 6, 2025
@josdejong
Copy link
Collaborator

Thanks for your explanation. I'm not sure either if this is a common need, I need to think this through a bit. You can currently only access data relative to the point where you are right now, but not from "somewhere else". There may be need to get data from a different place, like reading some config property from the root of the object or so. I'm not sure whether copy is the best solution for that. Maybe we need a new kind of getter, like getAbs(prop1, prop2, ...). Would a function like that be useful in your case?

@tlsproxy
Copy link
Author

tlsproxy commented Feb 7, 2025

Thanks for your explanation. I'm not sure either if this is a common need, I need to think this through a bit. You can currently only access data relative to the point where you are right now, but not from "somewhere else". There may be need to get data from a different place, like reading some config property from the root of the object or so. I'm not sure whether copy is the best solution for that. Maybe we need a new kind of getter, like getAbs(prop1, prop2, ...). Would a function like that be useful in your case?

If getAbs can obtain data starting from the root node of the original data regardless of any other processing methods used before it, I think it can meet my needs. In fact, initially, I was wondering if there was a function similar to "parent" that would allow me to search upwards. Later, I realized that the function could only get the processing result of the previous function, so I gave up and resorted to the passing method.

image

This is my current requirement. If you're not sure, I think we can wait and see if others have similar needs. For now, I'll use it in this way. Thank you.

@josdejong
Copy link
Collaborator

Thanks. You would indeed be able to get some data starting from the root.

One option would be a getAbs function, but another one would be to introduce a special value like .. to go to a parent, like get('..', 'image') (similar to how directories work on a computer). I still have to think through what is needed to implement such a feature, but I think we definitely need to implement one of those options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants