skip to Main Content

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": "-"
  }
}

2

Answers


  1. 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
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. You can deliver the elements to the respective objects by their common properties such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*ame": "&", // accumulate names under the common node
            "Phone|Email": { // the attribute named "Phone" OR "Email"
              "@": "Contact.&" // the value brought from the upper node while determining the key "Contact" for the object
            },
            "*": {
              "@": "Address.&"
            }
          }
        }
      }
    ]
    

    where ampersand operator replicates the value of the respective element

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search