Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to map data.code to each element in the body array using Jolt? #1273

Open
XyhVeryNB opened this issue Dec 23, 2024 · 1 comment
Open

Comments

@XyhVeryNB
Copy link

I have a JSON data structure that contains a data object. Inside the data object, there is a code field and a body array. Now, I would like to add the value of the code field to each element of the body array. Specifically, the value of code should be included along with the other fields of each object in the body array. How can I achieve this transformation using the Jolt tool?

Here is my input data:

{
"data": {
"code": "Xd001",
"body": [
{
"id": "1",
"name": "ls"
},
{
"id": "2",
"name": "zs"
}
]
}
}
I want the output to look like this:
{
"data": {
"body": [
{
"id": "1",
"name": "ls",
"code": "Xd001"
},
{
"id": "2",
"name": "zs",
"code": "Xd001"
}
]
}
}

@gbouget
Copy link

gbouget commented Dec 26, 2024

Jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "body": {
          "*": {
            "*": "data.body[&1].&",
            "@(2,code)": "data.body[&1].code"
          }
        }
      }
    }
  }
]

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

No branches or pull requests

2 participants