0

I need to transform JSON using jolt. Input JSON is as follows:

 {
  "items": [
    {
      "Group1": {
        "ABCCode": "3",
        "ABCDescription": "abcd"
      },
      "Group2": {
        "test2": [
          "123"
        ]
      }
    }
  ]
}

and I need the output as the following,

[
   {
      "test2Id": "123",
      "attrname": "ABCCode",
      "attrval": "3"
   },
   {
      "test2Id": "123",
      "attrname": "ABCDescription",
      "attrval": "abcd"
   }
]

How can I achieve this using jolt?

Dee
  • 31
  • 3

1 Answers1

0

This produces the output from the given input. You will probably need to tweak it, as you give it different types of input.

[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "Group1": {
            // match all keys below Group1
            "*": {
              // "$" means grab the key matched above, 
              //   and write it the the output as "attrname"
              "$": "[#2].attrname",
              // "@" means grab the value matched above
              "@": "[#2].attrval",
              // walk back up the match tree three levels
              //  walk back down the path "Group2.test2[0]"
              //  and write that value to the output
              "@(2,Group2.test2[0])": "[#2].test2Id"
            }
          }
        }
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22