Search code examples
jsontransformationjolt

transform input json in two different collections shares same property from input json


Sample input JSON

{
  "name": "XYZ",
  "lastName": "PQR",
  "address": "abc, mnop, 534291"
}

Expected output JSON with shared properties

{
  "employee": {
    "name": "XYZ",
    "lastName": "PQR",
    "address": "abc, mnop, 534291"
  },

  "address": {
    "name": "XYZ",
    "lastName": "PQR",
    "address": "abc, mnop, 534291"
  }
}

Solution

  • You can use * and @ wildcards together within a shift transformation like this :

    [
      {
        "operation": "shift",
        "spec": {
          "*": "address.&", // this needs an ampersand to replicate the whole content
          "@": "employee"   // while this one doesn't
        }
      }
    ]
    

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

    enter image description here

    or another alternative option might be :

    [
      {
        "operation": "shift",
        "spec": {
          "@": ["employee", "address"]
        }
      }
    ]
    

    the demo is :

    enter image description here