Thiyagaraj Narayanan
Thiyagaraj Narayanan

Reputation: 331

Jolt to handle a complex Nested JSON to flat JSON

Jolt spec to handle a nested array when the values are coming in a array

Input :

{
  "KUNNR": "123456789",
  "NAME1": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME2": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME3": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME4": "Milchwerke \"Mittelelbe\" GmbH",
  "LAND1": "DE",
  "VKORG": "1255",
  "VTWEG": "00",
  "SPART": "00",
  "PARVW": [
    "WE",
    "WE"
  ],
  "PARZA": [
    "000",
    "000"
  ],
  "KUNN2": [
    "0000045959",
    "0000045959"
  ]
}

Output Expected :

[
  {
    "KUNNR": "123456789",
    "NAME1": "Milchwerke \"Mittelelbe\" GmbH",
    "NAME2": "Milchwerke \"Mittelelbe\" GmbH",
    "NAME3": "Milchwerke \"Mittelelbe\" GmbH",
    "NAME4": "Milchwerke \"Mittelelbe\" GmbH",
    "LAND1": "DE",
    "VKORG": "1255",
    "VTWEG": "00",
    "SPART": "00",
    "PARVW": "WE",
    "PARZA": "000",
    "KUNN2": "0000045959"
  },
  {
    "KUNNR": "123456789",
    "NAME1": "Milchwerke \"Mittelelbe\" GmbH",
    "NAME2": "Milchwerke \"Mittelelbe\" GmbH",
    "NAME3": "Milchwerke \"Mittelelbe\" GmbH",
    "NAME4": "Milchwerke \"Mittelelbe\" GmbH",
    "LAND1": "DE",
    "VKORG": "1255",
    "VTWEG": "00",
    "SPART": "00",
    "PARVW": "WE",
    "PARZA": "000",
    "KUNN2": "0000045959"
  }
]

I'm trying to write a spec to do the below transformation using jolt transformation. I need to convert the flat json to nested Json

I am having some trouble with converting the flat JSON to Nested JSON. I have looked at examples and didn't get any closer as to what is mentioned above. I need to transform a JSON structure by using a JOLT spec. I use https://jolt-demo.appspot.com to test the following below.

Upvotes: 0

Views: 41

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

You can use the following transformation :

[
  {
    "operation": "shift",
    "spec": {
      "*": "Others.&", // The elements other than the arrays
      "PARVW|PARZA|KUNN2": "Arrays.&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Arrays": {
        "*": {
          "*": {
            "@3,Others": { "*": "[&2].&" },
            "@": "[&1].&2"
          }
        }
      }
    }
  },
  { // pick only one per each identical components of the arrays
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  }
]

Upvotes: 0

Related Questions