Home > Software design >  JOLT shift transformation: collect the items from all the levels without knowing how many levels int
JOLT shift transformation: collect the items from all the levels without knowing how many levels int

Time:10-31

I am trying to transform a JSON using Jolt transformation looking for some input here. I am trying to get inner items from all the levels into an one array. My goal is to get an array that contains a part of the items without knowing how many levels I have in the json into a list in one spen.

Here is my input and expected output:

Input:

{
  "id": 1,
  "item": [
    {
      "id": "1_1",
      "foo": {
        "id": 1232,
        "nn": "sdfsd"
      }
    }
  ]
}

Expected output: (list)

{
  "type" : [ "sdfsd" ]
}

My jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "item": {
            "*": {
              "item": {
                "*": {
                  "foo": {
                    "nn": "type"
                  }
                }
              },
              "foo": {
                "nn": "type"
              }
            }
          },
          "foo": {
            "nn": "type"
          }
        }
      }
    }
  }
]

My output:

{
  "type" : "sdfsd"
}

In case I've multiple items in the input I got a list, but if I've only one item not. Do you know how should I convert it to array anyway? But I need to do it only in a one spec - its possible?

CodePudding user response:

Just suffix each type literals, which represent keys, on the right hand side by square brackets, eg.converting them to type[] makes generating array results such as

{
  "type" : [ "sdfsd" ]
}

even if there were multiple objects composing item array, the resulting form is kept as the same and the result would yield such alike results

{
  "type" : [ "sdfsd", "dfsds", "fjghi", ... ]
}

depending on the input values

  • Related