0

I'm facing issue while adding new Items to an Array. I want to add new Items in address array. any help would be appreciate. Thanks in advance:

This is my code:

input.json

{
  "lines": [
    {
      "movement": {
        "source": {
          "node": "Org_Node",
          "address": {
            "addressLine1": "abc",
            "addressLine2": "def",
            "addressLine3": "eg",
            "addressLine4": "abc123"
          },
          "recipient": {
            "firstName": "Ravi",
            "middleName": "",
            "lastName": "krishna"
          },
          "contactInformation": {
            "mobileNo": "9687568965"
          }
        }
      }
    }
  ]
}

In output I want to add city& state: Expected output is:

{
  "address" : {
    "address1" : "abc",
    "address2" : "def",
    "address3" : "eg",
    "address4" : "abc123",
    "city":"ATP",
    "state":"AP"
  },
  "recipient" : {
    "firstName" : "Ravi",
    "middleName" : "",
    "lastName" : "krishna"
  },
  "contactInformation" : {
    "mobileNo" : "5036412733"
  }
}

I wrote following spec file to get my desired output,Please let me know where i have to modify I'm new to JOLT. spec.json file:

    [{
  "operation": "shift",
  "spec": {
    "lines": {
      "*": {
        "movement": {
          "source": {
            "address": {
              "addressLine*": "[#5].&1.address&(0,1)",
              "*": "[#5].&1.&"
            },
            "recipient": {
              "*": "[#5].&1.&"
            },
            "contactInformation": {
              "*": "[#5].&1.&"
            }
          }
        }
      }
    }
  }
}]
0

1 Answer 1

1

Spec

[
  {
    "operation": "shift",
    "spec": {
      "lines": {
        "*": {
          "movement": {
            "source": {
              // keeping the lines array
              // just keep the follow 3 fields
              "address|recipient|contactInformation": "lines[&3].&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      // default has a different syntax than shift.
      // You have to tell Default that lines is an array
      //  so that it can step into it correctly.
      "lines[]": {
        "*": {
          "address": {
            // apply cit and state defaults
            // not this is not in any way data driven
            // it will add these city and state defaults to everythhing
            // If you need it to be data driven, you have to deal with
            //  that before or after Jolt
            "city": "ATP",
            "state": "AP"
          }
        }
      }
    }
  }
]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Milo,thanks,it is working fine but I need to change key names as well in address array from 'addressLine1' to address1.
Add another shift, at the end, to fix those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.