Home > Software engineering >  what is the significance of [] in RHS in JOLT specification?
what is the significance of [] in RHS in JOLT specification?

Time:01-24

I am new to JOLT. I am creating a JOLT specification to parse a JSON from one format to another. However when i use the RHS as "&1.name" it doesn't work as expected and creates a different format. But when i use "[&1].name", it works.

I cannot share the data due to policy constraints. Giving an example

eg. Input JSON:

[
  {
    "name": "my name1"
  },
  {
    "name": "my name2"
  }
]

JOLT spec (Not working) :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "&1.name"
      }
    }
  }
]

JOLT spec (working) :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "[&1].name"
      }
    }
  }
]

Please help me understand the purpose of [] on RHS.

CodePudding user response:

You can even rephrase by replacing the name on the right hand side by an ampersand & as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "[&1].&"
      }
    }
  }
]

in order to replicate it

Prepending both [&1] and &1 targets the same level which's going up the tree 1 level in order to reach the level of the indexes of the outermost array by traversing opening curly brace(s) ({) once. But the first one yields arraywise result instead of index numbers 0,1,2 ... those are generated by the second one.

CodePudding user response:

Both two spec is valid.

&1: get 1 level up key.

[&1]: get 1 level up index.

For example in your spec, &1 means 0 and 1 as key and your output can be like this:

{
  "0": {
    "name": "my name1"
  },
  "1": {
    "name": "my name2"
  }
}

And when you using the [&1] means 0 and 1 as index of array and your output can be like this:

[
  {
    "name": "my name1"
  },
  {
    "name": "my name2"
  }
]
  • Related