bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

Splitting a string which is inside an array of objects #1241

Closed spnathan-ce closed 8 months ago

spnathan-ce commented 8 months ago

I need help in Splitting a String and get the 0th index, the String is contained in a list of objects. The below are the details

Input JSON: { "data": [ { "key": "name", "value": "Nathan" }, { "key": "token", "value": "123#456#789" } ] }

Rule I am using:

[ { "operation": "modify-overwrite-beta", "spec": { "data": { "*": { "key": { "token": { "list": "=split('#',@(2,value))" } } } } } }, { "operation": "shift", "spec": { "data": { "*": { // I need in help in accessing the list value I created in the modify-overwrite-beta operation } } } } ]

Expected output: { "tokenValue":"123" }

Please help on this

gbouget commented 8 months ago

There are several solutions, but this is probably the easiest to understand:

[
  { // extract token value
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "key": {
            "token": {
              "@(2,value)": "tokenValue"
            }
          }
        }
      }
    }
  },
  { // split on # 
    "operation": "modify-overwrite-beta",
    "spec": {
      "tokenValue": "=split('#',@0)"
    }
  },
  { // get first element of array
    "operation": "modify-overwrite-beta",
    "spec": {
      "tokenValue": "=firstElement"
    }
  }
]

image

spnathan-ce commented 8 months ago

Thanks a lot @gbouget

spnathan-ce commented 8 months ago

Resolved