Search code examples
jsontransformationjolt

Building a jolt transformation that duplicates values


I'm trying to build a jolt transformer that goes from

{
  "name": "name",
  "tags": [
    "value1",
    "value2"
  ]
}

(for n values) to

{
  "options": [
    {
      "key": "value1",
      "value": "value1"
    },
    {
      "key": "value2",
      "value": "value2"
    }
  ]
}

I've tried a bunch of variants, and I can move the items of the input array into various parts of the output, but I can't figure out how to duplicate the value of the array item into the object in the output.


Solution

  • Description inline:

    [
      {
        "operation": "shift",
        "spec": {
          //match tags
          "tags": {
           //for each item in array
            "*": {
              //Use value from the array (@) and set it as key and value
              //Using array position in [&1]
              "@": ["options.[&1].key", "options.[&1].value"]
            }
          }
        }
      }
    ]