bazaarvoice / jolt

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

How to "shift" only one element on array using index? #1207

Open anaconda875 opened 1 year ago

anaconda875 commented 1 year ago

My input json looks like:

{
  "id" : "eea1c86e-e4e2-36df-a775-ad44a6d2d94e",
  "d" : {
    "flag" : 0,
    "a0" : [ {
      "a1" : 0,
      "a2" : -1,
      "a3" : 7,
    },
    {
      "a1" : 2,
      "a2" : -3,
      "a3" : 4,
    } ]
  }
}

I want to transform it to:

{
  "dg" : {
    "gi" : 7          //7 is from d.a0.[first element from array].a3
  }
}

So i write spec like this

[
  {
    "operation": "shift",
    "spec": {
      "d": {
        "a0": {
          "[0]": {
            "a3": "dg.gi"
          }
        }
      }
    }
  }
]

Java code:

List<Object> specs = JsonUtils.classpathToList(spec);
Chainr chainr = Chainr.fromSpec(specs);
// transform the message
return (Map<String, Object>) chainr.transform(inputJSON);

But it returns null and I don't know why

I'm new to Jolt so I don't have much knowledge about it

Please help, thank you