sudheer
sudheer

Reputation: 33

JSON to JSON using Jolt Spec, split String to array

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

  ]
}

Upvotes: 3

Views: 4826

Answers (1)

Lemmerich
Lemmerich

Reputation: 1292

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": ""
        }
      }
    }
    }
]

Upvotes: 4

Related Questions