bazaarvoice / jolt

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

Using an object as a map to insert value inside array with the same key #1082

Open mllenz opened 3 years ago

mllenz commented 3 years ago

Hi, I have a list of entities with some unique id and unique reference. But I need to insert the value of a dictionary (in a field named orig) matching the key of that dictionary with ref field of the list. When the key isn't found inside the list, the orig can be null. This is the input:

{ "dict": { "F": "3", "A": "6", "G": "7" }, "list": [ { "id": 5, "ref": "A" }, { "id": 3, "ref": "G" }, { "id": 2, "ref": "F" }, { "id": 7, "ref": "F" } ] }

And this is the output that I want:

{ "dict": { "F": "3", "A": "6", "G": "7" }, "list": [ { "id": 5, "ref": "A", "orig": "6" }, { "id": 3, "ref": "G", "orig": "7" }, { "id": 2, "ref": "F", "orig": "3" }, { "id": 7, "ref": "F", "orig": null } ] }

lucioalmeida commented 3 years ago

Fala maikon de boa. Olha a solução ai.

[
  {
    "operation": "shift",
    "spec": {
      "dict": "dict",
      "list": {
        "*": {
          "ref": {
            "*": {
              "@1": "list[&3].ref",
              "@(4,dict.&0)": "list[&3].orig"
            }
          },
          "*": "list[&1].&0"
        }
      }
    }
  }
]