Home > database >  Transformation List of Lists with Jolt
Transformation List of Lists with Jolt

Time:03-24

I have one JSON like this:

{
  "item1": [
    "serv1",
    "serv2",
    "serv3"
  ],
  "item2": [
    {
      "obj": [
        "matriz1",
        "matriz2",
        "matriz3"
      ]
    },
    {
      "obj": [
        "matriz4",
        "matriz5",
        "matriz6"
      ]
    }
  ]
}

And I need to obtain this output:

{
  "item1": [
    "serv1",
    "serv2",
    "serv3"
  ],
  "item2": [
    [
      "matriz1",
      "matriz2",
      "matriz3"
    ],
    [
      "matriz4",
      "matriz5",
      "matriz6"
    ]
  ]
}

I'm trying with this JOLT spec:

[
  {
    "operation": "shift",
    "spec": {
      "item1": "item1",
      "item2": {
        "*": {
          "obj": "item2"
        }
      }
    }
  }
]

But the output contains 3 elements of the first array, and then the second array like I want.

{
  "item1": [
    "serv1",
    "serv2",
    "serv3"
  ],
  "item2": [
    "matriz1",
    "matriz2",
    "matriz3",
    [
      "matriz4",
      "matriz5",
      "matriz6"
    ]
  ]
}

What could be the correct JOLT spec to obtain the correct output?

CodePudding user response:

You can use a shift transformation spec such as

[
  {
    "operation": "shift",
    "spec": {
      "item1": "&",
      "item2": {
        "*": {
          "obj": "&2[&1]"
        }
      }
    }
  }
]

where no need to repeat the key names, rather use substitutions through use of ampersands.

  • & replaces current key names
  • &2 replaces "item2", since needed to go 2 levels up to grab the name
  • &2[&1] represents elements of the obj list nested within the key name "item2"

the demo on the site enter image description here

  • Related