Transforming JsonArray to shift keys into inner JsonArray using Jolt Transform

28 Views Asked by At

I am having trouble getting the desired Output using Jolt Transform.

My input JsonArray looks like this:

[
  {
    "from": [
      {
        "area1": 1
      },
      {
        "area2": 1
      },
      {
        "area3": 1
      }
    ],
    "id": 111,
    "to": "destination1"
  },
  {
    "from": [
      {
        "area1": 2
      },
      {
        "area2": 2
      },
      {
        "area3": 2
      }
    ],
    "id": 222,
    "to": "destination2"
  }
]

I am using this Jolt Spec to create a JsonArray for every JsonObject in the JsonArray "from" and want to include the where "id" and "to" keys:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "from": {
          "*": {
            "@": "[&]",
            "@(2,id)": "[&].id",
            "@(2,to)": "[&].to"
          }
        }
      }
    }
  }
]

This is the output:

[
  [
    {
      "area1": 1,
      "id": 111,
      "to": "destination1"
    },
    {
      "area1": 2
    }
  ],
  [
    {
      "area2": 1,
      "id": 111,
      "to": "destination1"
    },
    {
      "area2": 2
    }
  ],
  [
    {
      "area3": 1,
      "id": 111,
      "to": "destination1"
    },
    {
      "area3": 2
    }
  ]
]

My desired output would look something like that:

[
  {
    "area1": 1,
    "id": 111,
    "to": "destination1"
  },
  {
    "area1": 2,
    "id": 222,
    "to": "destination2"
  },
  {
    "area2": 1,
    "id": 111,
    "to": "destination1"
  },
  {
    "area2": 2,
    "id": 222,
    "to": "destination2"
  },
  {
    "area3": 1,
    "id": 111,
    "to": "destination1"
  },
  {
    "area3": 2,
    "id": 222,
    "to": "destination2"
  }
]

I am not sure, why the "id" and "to" key aren't included from the second JSON Object of the input. Does anyone know how to fix the Jolt to get the desiered output?

1

There are 1 best solutions below

0
Barbaros Özhan On BEST ANSWER

First of all, no need to write the outer attributes one by one

I'd suggest the following shift as the first spec, If there was only one nested object within the outermost array :

  {
    "operation": "shift",
    "spec": {
      "*": {
        "from": {
          "*": {
            "@2": { // replicate the respective values from 2 upper level
              "*": "[&1].&"
            },
            "*": "[&1].&" // replicate the attributes under objects those are under "from" arrays
          }
        }
      }
    }
  }

but because you have twice, then need to separate them, to do this : Prefix the identifiers by &4 and &3 respectively as in the following complete transformation :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "from": {
          "*": {
            "@2": {
              "*": "&4[&1].&"
            },
            "*": "&3[&1].&"
          }
        }
      }
    }
  },
  { // get rid of the generated inner "from" arrays
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "from": ""
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

the demo on the site https://jolt-demo.appspot.com/ is :

enter image description here