bazaarvoice / jolt

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

Help with Jolt transformation spec #1259

Closed cmclopes closed 4 months ago

cmclopes commented 5 months ago

I'm sure I'm missing something, but given this JSON input:

{
  "input": {
    "payment": [
      {
        "id": "rem_X_nSRHt1KrIEC4EAkruYrpG-Osw",
        "status": "SUCCESS"
      },
      {
        "id": "rem_X_asdasdasdpG-asd2",
        "status": "SUCCESS"
      }
    ]
  },
  "temp": [
    {
      "transferId": "rem_X_nSRHt1KrIEC4EAkruYrpG-Osw",
      "recipientId": "12345678"
    },
    {
      "transferId": "rem_X_asdasdasdpG-asd2",
      "recipientId": "9876543"
    }
  ]
}

What would the jolt spec be to achieve this output:

{
    "payment": [
      {
        "id": "rem_X_nSRHt1KrIEC4EAkruYrpG-Osw",
        "status": "SUCCESS",
        "recipientId": "12345678"
      },
      {
        "id": "rem_X_asdasdasdpG-asd2",
        "status": "SUCCESS",
        "recipientId": "9876543"
      }
    ]
  }

The idea is to look into the temp array where the payment[*].id == temp[*].transferId and add the temp[*].recipientId into the respective object in the payment array.

LucaBiscotti commented 5 months ago

Hi @cmclopes, this JOLT should work

  {
    "operation": "shift",
    "spec": {
      "input": {
        "payment": {
          "*": {
            "*": "&2.[&1].&"
          }
        }
      },
      "temp": {
        "*": {
          "recipientId": "payment.[&1].&"
        }
      }
    }
  }
]

If there is anything unclear, feel free to ask me or, if you want, go check a guide i wrote on how to use jolt in my github :)

cmclopes commented 4 months ago

@LucaBiscotti Thank you!