bazaarvoice / jolt

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

How to compare fields between 2 JSON objects and create a response JSON in Jolt #1260

Open Madhavi56 opened 3 months ago

Madhavi56 commented 3 months ago

Request:

{
  "request1": {
    "firm": {
      "item": [
        {
          "Du": "1234",
          "Gu": "123",
          "DuNm": "QWERTy",
          "Don":{
            "donType":"Modern",
            "donValue":"0002"
          }

        }
      ]
    }
  },
  "request2": {
    "firm": {
      "item": [
        {
          "Du": "999999999",
          "Gu": "999999999",
          "GuNm": "Sample",
          "Mad":"Fluffy",
          "isInt":true,
          "Dusky":{
            "duskyType":"Modern",
            "duskyValue":"0002"
          }
        }
      ]
    }
  }
}

Desired Response:

{
  "original": {
    "Du": "999999999",
    "Gu": "999999999",
    "DuNm": "",
      "Don":{}
  }
}

Jolt:

Jolt I am trying so far

[
  {
    "operation": "shift",
    "spec": {
      "request1": {
        "firm": {
          "item": {
            "*": {
              "*": {
                "@5,request2.firm.item": {
                  "*": {
                    "@(&1)": "original.&2"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Output I am getting:

{
  "original" : {
    "Du" : "999999999",
    "Gu" : "999999999"
  }
}

I want to check if the field in request1 is there or not in request2, if it is then add it in the response with the value present in request2. if the String field is not there in request2 then the its value should go as "" in output.if the Object field is not there in request2 then the its value should go as {} in output. The request1 and request2 can have multiple different fields at each time.