Home > database >  Flatten Array using Jolt Transformation
Flatten Array using Jolt Transformation

Time:06-27

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

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "[&1].name",
        "id": "[&1].id",
        "lang": {
          "*": {
            "primary": "lang\\[&2\\]\\.primary",
            "secondary": "lang\\[&2\\]\\.secondary"
          }
        }
      }
    }
  }
]

enter image description here

CodePudding user response:

  • 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 enter image description here

  • Related