3

I need to use jolt transform to do the below JSON transformation.

Need to split the "PID3" value in input Json to array of key value pairs in output Json

Input JSON

{ "PID1": "value1", "PID2": "value2", 
  "PID3": "k1^value1~k2^value2~k3^value3" # It is Dynamic might contain multiple key value pair seperated by ~ 
}

Output JSON

{
  "PID1": "value1",
  "PID2": "value2",
  "PID3": [
  {
  "key":"k1",
  "value":"value1"
  },
  {
  "key":"k2",
  "value":"value2"
  },
  {
  "key":"k3",
  "value":"value3"
  }

-- multiple based on the input string

  ]
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
sudheer
  • 33
  • 1
  • 4

1 Answers1

4

This should do the trick:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "PID3": "=split('~', @(1,PID3))"
    }
    },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "PID3": {
        "*": "PID3.[&].part"
      }
    }
    },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "PID3": {
        "*": {
          "part": "=split('\\^', @(1,part))",
          "key": "@(1,part[0])",
          "value": "@(1,part[1])"
        }
      }
    }
    },
  {
    "operation": "remove",
    "spec": {
      "PID3": {
        "*": {
          "part": ""
        }
      }
    }
    }
]
Lemmerich
  • 1,222
  • 1
  • 10
  • 13