Search code examples
jsonjolt

JOLT Remove Object in Array using values of another field


I struggle to find the right JOLT spec to transform my JSON input properly.

I want to achieve the following:

  1. Check for each ratingResult the value of corporateFlag.
  2. If the value is true then the object ratingValue for the underlying ratingResult should be removed.
  3. Further the rest of the input JSON should remain as is.

I have the following Input JSON

{
  "rating": {
    "id": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
    "ratingResponse": {
      "ratingId": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
      "ratingResults": [
        {
          "customerId": "123",
          "corporateFlag": false,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "ratingValue": {
            "rating": "AAA",
            "ratingPoints": 123
          },
          "role": "Applicant"
        },
        {
          "customerId": "456",
          "corporateFlag": true,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "ratingValue": {
            "rating": "B",
            "ratingPoints": 423
          },
          "role": "Company"
        }
      ]
    },
    "links": []
  }
}

I expect the following output

Expected Output JSON

{
  "rating": {
    "id": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
    "ratingResponse": {
      "ratingId": "46787db9-a3fd-4f7f-9bc8-572604c9a267",
      "ratingResults": [
        {
          "customerId": "123",
          "corporateFlag": false,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "ratingValue": {
            "rating": "AAA",
            "ratingPoints": 123
          },
          "role": "Applicant"
        },
        {
          "customerId": "456",
          "corporateFlag": true,
          "decision": {
            "code": "R",
            "description": "Refer"
          },
          "role": "Company"
        }
      ]
    },
    "links": []
  }
}

I have tried the following JOLT spec so far

[
  {
    "operation": "shift",
    "spec": {
      "rating": {
        "*": "rating.&",
        "ratingResponse": {
          "*": "rating.ratingResponse.&",
          "ratingResults": {
            "*": {
              "*": "rating.ratingResponse.ratingResults[&1].&",
              "ratingValue": {
                "@(2,corporateFlag)": {
                  "false": {
                    "@0": "rating.ratingResponse.ratingResults[&1].ratingValue"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

But this removes always the ratingValue independent of the value of the corporateFlag.

The result is

{
  "rating" : {
    "id" : "46787db9-a3fd-4f7f-9bc8-572604c9a267",
    "ratingResponse" : {
      "ratingId" : "46787db9-a3fd-4f7f-9bc8-572604c9a267",
      "ratingResults" : [ {
        "customerId" : "123",
        "corporateFlag" : false,
        "decision" : {
          "code" : "R",
          "description" : "Refer"
        },
        "role" : "Applicant"
      }, {
        "customerId" : "456",
        "corporateFlag" : true,
        "decision" : {
          "code" : "R",
          "description" : "Refer"
        },
        "role" : "Company"
      } ]
    },
    "links" : [ ]
  }
}

Any tips or tricks how to achieve the desired result?


Solution

  • You can

    • reduce the level of "@2,corporateFlag" by one, eg. "@1,corporateFlag"
    • use ampersand symbols such as &, &1, &2 ... rather than literals

    So, you can retry with the following one :

    [
      {
        "operation": "shift",
        "spec": {
          "rating": {
            "*": "&1.&",
            "ratingResponse": {
              "*": "&2.&1.&",
              "ratingResults": {
                "*": {
                  "*": "&4.&3.&2[&1].&",
                  "ratingValue": {
                    "@1,corporateFlag": {
                      "false": { "@2": "&7.&6.&5[&4].&3" }
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    enter image description here