Search code examples
jsonjolt

reorganize some keys element in json subarray with JOLT


I have simple JSON and I want to reorganize the keys with JOLT transformation: Input JSON looks like this:

[
  {
    "Firstname": "John",
    "LastName": "JohnLastName",
    "City": "TestCity",
    "PostCode": "25466",
    "Street": "85.",
    "HousNumber": "5",
    "Phone": "0054565685",
    "Email": "-"
  }
]

and wished output should be:

{
  "Firstname": "John",
  "LastName": "JohnLastName",
  "Address": {
    "City": "TestCity",
    "PostCode": "25466",
    "Street": "85",
    "HousNumber": "5"
  },
  "Contact": {
    "Phone": "0054565685",
    "Email": "-"
  }
}

Solution

  • You can use the below Jolt Spec

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "City": "Address.&",//Get the key and value where the key is City and place them within the Address map 
            "PostCode": "Address.&",
            "Street": "Address.&",
            "HousNumber": "Address.&",
            "Phone": "Contact.&",
            "Email": "Contact.&",
            "*": "&"// Get the remaining fields of the input JSON
          }
        }
      }
    ]