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

Flatten Array using Jolt Transformation

This is input JSON which has array, which can contain one or more objects.
inside the object lang is array. It will always have one object which is fix. I want to flatten it.

[
  {
    "name": "James",
    "id": 1,
    "lang": [
      {
        "primary": "python",
        "secondary": "c++"
      }
    ]
  },
  {
    "name": "Kevin",
    "id": 2,
    "lang": [
      {
        "primary": "scala",
        "secondary": "java"
      }
    ]
  }
]

I want to flatten the JSON like this – expected output

[
  {
    "name": "James",
    "id": 1,
    "primary": "python",
    "secondary": "c++"
  },
  {
    "name": "Kevin",
    "id": 2,
    "primary": "scala",
    "secondary": "java"
  }
]

Im tryin to build specification like this but its not working

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": {
      "*": {
        "name": "[&1].name",
        "id": "[&1].id",
        "lang": {
          "*": {
            "primary": "lang\\[&2\\]\\.primary",
            "secondary": "lang\\[&2\\]\\.secondary"
          }
        }
      }
    }
  }
]

enter image description here

>Solution :

  • No need to escape for the square bracket characters
  • Increment from [&2] to [&3] as the value on the right hand
    side(in order to traverse the colon following the innermost "*" )
  • Try to think symbolically(no need to repeat the literals, but use
    ampersands to replicate them)

such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&", // the rest of the attributes(other than "lang" array)
        "lang": {
          "*": {
            "*": "[&3].&"
          }
        }
      }
    }
  }
]

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