Search code examples
jsonconfigjolt

If property already exists in primary object, should not refer from secondary object in Jolt


In the given JSON input, the primary object is "Account." The objective is to create a Jolt specification that selectively picks properties from the "SystemResponse" object only if they do not already exist in the "Account" object. For instance, if "serviceFlag" is present in "SystemResponse" but not in "Account," it should be added to "Account." However, properties like "resellerFlag" (which maps to "isReseller" in "Account") and others should be ignored if they already exist in the "Account" object.

Please help me to achieves this conditional property selection.

Input.json

{
  "Account": {
    "AccountInfo": {
      "isReseller": 10,
      "poRequired": 10
    }
  },
  "SystemResponse": {
    "resellerFlag": "false",
    "poRequiredFlag": "false",
    "serviceFlag": "true"
  }
}

Output.json

[
  {
    "operation": "shift",
    "spec": {
      "Account": {
        "AccountInfo": {
          "*": "Account.AccountInfo.&"
        }
      },
      "SystemResponse": {
        "@resellerFlag": {
          "false": {
            // Check if isReseller doesn't already exist in Account
            "Account.AccountInfo.isReseller": {
              "#0": "partyAccount.eqxAccountInfo.isReseller"
            }
          }
        },
        "poRequiredFlag": {
          "false": {
            // Check if poRequired doesn't already exist in Account
            "@Account.AccountInfo.poRequired": {
              "#0": "Account.AccountInfo.poRequired"
            }
          }
        },
        "serviceFlag": {
          "false": {
            // Check if poRequired doesn't already exist in Account
            "@Account.AccountInfo.isService": {
              "#0": "Account.AccountInfo.isService"
            }
          },
          "true": {
            // Check if poRequired doesn't already exist in Account
            "@Account.AccountInfo.isService": {
              "#1": "Account.AccountInfo.isService"
            }
          }
        }
      }
    }
  }
]

Actual output

{
  "Account" : {
    "AccountInfo" : {
      "isReseller" : 10,
      "poRequired" : 10
    }
  }
}

Expected output json

{
  "Account" : {
    "AccountInfo" : {
      "isReseller" : 10,
      "poRequired" : 10,
      "isService" : 1
    }
  }
}


Solution

  • You might use ~ operator in order to handle such an issue. That means if the respective attribute doesn't exist or is null, then match with the returned value applying such as in the follow transformation

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "Account": {
            "AccountInfo": {
              "~isReseller": "@(3,SystemResponse.resellerFlag)",
              "~poRequired": "@(3,SystemResponse.poRequiredFlag)", 
              "~serviceFlag": "@(3,SystemResponse.serviceFlag)"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "Account": {
            "AccountInfo": {
              "*": {
                "true": {
                  "#1": "&4.&3.&2"
                },
                "false": {
                  "#0": "&4.&3.&2"
                },
                "*": { // leave the rest as they are
                  "@1": "&4.&3.&2"
                }
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "Account": {
            "AccountInfo": {
              "*": "=toInteger" // convert quoted integers unquoted
            }
          }
        }
      },
      { // rename the attributes as desired
        "operation": "shift",
        "spec": {
          "Account": {
            "AccountInfo": {
              "isReseller": "&2.&1.&",
              "poRequired": "&2.&1.isRequired",
              "serviceFlag": "&2.&1.isService"
            }
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ with a slightly changed input folows :

    enter image description here