How to delete the text befor the symbol ( _ ) , using jolt json?

Viewed 41

i have an input JSON file , which have some attributes those contains some informations which i want to delete on the output

( example input : Hello_World => output = World )

For example this is the input :

{
  "test": "hello_world"
}

and this is output needed :

{
  "test" : "world"
}

the result derived from the jolt spec i tried is not even close, this is what i tried :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "test": "=test.substring(test.indexOf(_) + 1)"
    }
  }
]

Sorry im i a newbie at jolt, just started it.

1 Answers

You can split by _ character within a modify transformation spec, and pick the one with the index 1 in order to display the last component of the arrah considering the current case of having a single underscore such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=split('_', @(1,&))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "1": "&1" // &1 represents going one level up to reach to the level of the label "test" to replicate it
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is : enter image description here

Considering an input with attribute having value more than one underscore :

{
  "test": "how_are_you"
}

[More generic] solution would be as follows :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "test_": "=split('_', @(1,test))",
      "test": "=lastElement(@(1,test_))"
    }
  },
  {
    // get rid of the extra generated attribute
    "operation": "remove",
    "spec": {
      "test_": ""
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is : enter image description here

Related