Home > Back-end >  Is it possible to convert json object into array in JOLT with condition?
Is it possible to convert json object into array in JOLT with condition?

Time:10-19

I need to wrap my json object into json array and if input is already an array juts leave as it is. Is it possible to do that JOLT ? Thanks for your help in advnace.

Scenario 1:

Input

{
  "primary": 5,
  "quality": 4,
  "design": 5
}

Output

[
  {
    "primary": 5,
    "quality": 4,
    "design": 5
  }
]

Scenario 2:

Input

[
  {
    "primary": 5,
    "quality": 4,
    "design": 5
  },
  {
    "primary": 5,
    "quality": 4,
    "design": 5
  }
]

Output

[
  {
    "primary": 5,
    "quality": 4,
    "design": 5
  },
  {
    "primary": 5,
    "quality": 4,
    "design": 5
  }
]

Thanks Mahendra

CodePudding user response:

You can determine a value(val) by going leaf(innermost) level of the tree using

"*": {
       "*": "val"
     }

whether to return null or not in order to separate the inputs by their depth, while keeping the original input value by using

"@": "input"

then default the value null to "0" in order to be able to use within the conditional for the next transformation spec such as

[
  {
    "operation": "shift",
    "spec": {
      "@": "input",
      "*": {
        "*": "val"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "val": ["=toString", ""],
      "val1": "=size(@(1,val))",
      "val2": "=toString(@(1,val1))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "val2": {
        "0": {
          "@(2,input)": "[]"
        },
        "*": {
          "@(2,input)": ""
        }
      }
    }
  }
]
  • Related