bazaarvoice / jolt

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

Add Value to every List Entry #1247

Closed Pytry closed 3 months ago

Pytry commented 3 months ago

Given The Input:

{
  "rate": 3.5,
  "items": [
    {
      "a": "a1",
      "b": "b1"
    },
    {
      "a": "a2",
      "b": "b2"
    }
  ]
}

The output should be:

{
  "results" : [ 
    {
      "a" : "a1",
      "b" : "b1",
      "RATE" : 3.5
    }, 
    {
      "a" : "a2",
      "b" : "b2",
      "RATE" : 3.5
    } 
  ]
}

My current chain is:

[
  {
   "operation": "shift",
   "spec": {
     "items": {
       "*": {
         "a": "results[&1].a",
         "b": "results[&1].b",
         "&3.rate": "results[&1].RATE"
       }
     }
   }
  }
]

The intent of "&3.rate" was to traverse back to the root andis to grab the value of a field off of the root. The number of entries in "items" cannot be known before hand.

Is there a way to accomplish this?

bobeal commented 3 months ago

You have to use the @ operator instead:

[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "a": "results[&1].a",
          "b": "results[&1].b",
          "@(2,rate)": "results[&1].RATE"
        }
      }
    }
  }
]
Pytry commented 3 months ago

You have to use the @ operator instead:

It worked! I could of sworn I tried that already:P Thank you so much for the help:)