bazaarvoice / jolt

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

Split the value and fetch by index #1232

Closed G-Balamurugan closed 9 months ago

G-Balamurugan commented 9 months ago

Why it is not possible to fetch the element from the split list Input : { "fullName": "hello_world_bye" }

Rule : [ { "operation": "modify-overwrite-beta", "spec": { "fullName": "=split('_', @(1, fullName))", "a": "=fullName[1]", "b": "=fullName[2]" } }, { "operation": "remove", "spec": { "fullName": "" } } ]

here I am trying to split the element and trying to fetch , particular index element alone How to perform that

bobeal commented 9 months ago

Hi @G-Balamurugan,

You can achieve it with such a transformation:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "fullName": "=split('_', @(1,fullName))",
      "a": "=elementAt(0,@(1,fullName))",
      "b": "=elementAt(1,@(1,fullName))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "fullName": ""
    }
  }
]

PS: be careful with the extra space after the comma in @(1, fullName), it will not work

G-Balamurugan commented 9 months ago

Thank you