Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transformation List of Lists with Jolt

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "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?

>Solution :

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 http://jolt-demo.appspot.com/ is :

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading