Home > other >  Tranforming an array of JSONs using JOLT
Tranforming an array of JSONs using JOLT

Time:10-07

I have a list of JSONs as below:

[
  {
    "id": 1,
    "name": "{a}"
  },
  {
    "id": 2,
    "name": "{b,c}"
  },
  {
    "id": 3,
    "name": "{d,e}"
  }
]

The idea is to transform this list and get the following as the output:

[
  {
    "id": 1,
    "name": "{a}",
    "tmp": "a"
  },
  {
    "id": 2,
    "name": "{b,c}",
    "tmp": "b,c"
  },
  {
    "id": 3,
    "name": "{d,e}",
    "tmp": "d,e"
  }
]

Can anyone help on this ?

What spec to use really confuses me here, is it Shift Or Default Or Modify ?

Thanks in advance !!

CodePudding user response:

You can consecutively use split along with the characters { and }, then join functions within a modify transformation spec, and then remove extra generated attributes by using a remove spec such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "tmp0": "=split('\\{', @(1,name))",
        "tmp1": "=join('', @(1,tmp0))",
        "tmp2": "=split('\\}', @(1,tmp1))",
        "tmp": "=join('', @(1,tmp2))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "tmp*": ""
      }
    }
  }
]

the demo on the site enter image description here

  • Related