Search code examples
javajsonjolt

Jolt spec to get the details using Dynamic ID from json


enter image description hereGiven dynamic accountId return the onlineId and signinId for that specific user.

Input JSON:

[
  {
    "request": {
      "body": {
        "inputAccountId": "1234"
      }
    }
  },
  {
    "accountId": "1234",
    "ageGroup": 3,
    "role": 1,
    "gender": "f",
    "signinId": "aa@aa.com",
    "onlineId": "one"
  },
  {
    "accountId": "1122",
    "ageGroup": 3,
    "role": 2,
    "gender": "f",
    "signinId": "bb@aa.com",
    "onlineId": "two"
  },
  {
    "accountId": "2211",
    "ageGroup": 1,
    "role": 1,
    "gender": "f",
    "signinId": "cc@aa.com",
    "onlineId": "three"
  }
]

If accountId = 1234 or accountId could be any account either 1122, or 2211 as well based on use case, then return the output JSON for that accountId.

Jolt:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "request": {
        "inputAccountId": "@(3,request.body.inputAccountId)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "request": {
        "inputAccountId": "requestedId"
      },
      "1": {
        "*": {
          "accountId": {
            "requestedId": {
              "onlineId": "onlineId",
              "signinId": "signInId"
            }
          }
        }
      }
    }
  }
]

I am not able to get the above code working.. The Java throws error - java.lang.NullPointerException" when reading the JOLT Spec file.

I also tried ${requestedId} but it goes as numeric, so I added it as requestedId which throws nullPointer exception.


Solution

  • You can use the following transformation specs :

    [
      { // set values of the object keys to their accountIds
        // while taking out the value of request.body.inputAccountId
        "operation": "shift",
        "spec": {
          "*": {
            "request": {
              "body": {
                "inputAccountId": "inputId"
              }
            },
            "*": {
              "@": "@2,accountId.&"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "inputId": {
            "*": {
              "@2,&": "[]" // traverse 2 levels to reach 
                             // the value of "inputId" in order
                             // to match with the value of
                             // object keys
            }
          }
        }
      }
    ]