Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON to JSON using Jolt Spec, split String to array

Tags:

json

jolt

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

  ]
}
like image 701
sudheer Avatar asked Oct 26 '25 07:10

sudheer


1 Answers

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": ""
        }
      }
    }
    }
]
like image 52
Lemmerich Avatar answered Oct 28 '25 20:10

Lemmerich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!