Home > Blockchain >  Jolt transform array to dissipate an outer attribute to each sub-object of an array
Jolt transform array to dissipate an outer attribute to each sub-object of an array

Time:06-30

I want to apply a Jolt Transformation, but this is still cloudy on my side:

This is my input data:

{
  "results": 1,
  "data": [
    {
      "detail": [
        {
          "num": "140"
        },
        {
          "num": "158"
        },
        {
          "num": "180"
        },
        {
          "num": "183"
        },
        {
          "num": "213"
        }
      ],
      "code": "01007"
    }
  ],
  "response_code": 200
}

My desired output:

[
  {
    "code": "01007",
    "num": "140"
  },
  {
    "code": "01007",
    "num": "158"
  },
  ....
  {
    "code": "01007",
    "num": "213"
  }
]

And my trial for the JOLT specification so far, I do not understand how to add a custom field to all elements of the list:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "detail": {
            "*": {
              "code": "[&1].code",
              "@": "[&1]"
            }
          }
        }
      }
    }
  }
]

CodePudding user response:

You can use the shift transformation like this

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "detail": {
            "*": {
              "@(2,code)": "[&1].code", // to go 2 levels up to reach the level of the attribute "code"
              "*": "[&1].&" // to get the values of the existing attributes within the current object without object wrapper
            }
          }
        }
      }
    }
  }
]

applying just some slight changes.

the demo on the site enter image description here

  • Related