Search code examples
javajsonsubstringjolt

Extracting a substring from multiple values in A JSON using JOLT spec


I have below json as input and I want to extract a substring from all the values. This targeted substring lies between a colon and a comma.

Input JSON:

{
  "key1": "ppp:qqq,rrr,",
  "key2": "111:222,",
  "key3": "aaa:bbb,"
}

Expected JSON output:

{
  "key1": "qqq,",
  "key2": "222",
  "key3": "bbb"
}

My current JOLT spec is working but only for the first key.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*:*,*": {
          "$(0,2)": "key1"
        }
      }
    }
  }
]

Current output:

{
  "key1": "qqq"
}

I need to modify my spec so that all keys present in the input json can be processed with the desired transformation.


Solution

  • Your approach, is pretty good, which would be improved like the one below :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*:*,": {
              "@|$(0,2)": "&2"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=split(',',@(1,&))"
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "*": "ONE"
        }
      }
    ]
    

    where the first spec(shift) returns

    {
      "key1" : "qqq,rrr",
      "key2" : "222",
      "key3" : "bbb"
    }
    

    then we extract the piece before the comma(s) if there's any(in this case exists only for key-value pair "key1" : "qqq,rrr" )