Home > Software engineering >  Jolt Spec for Json that may or may not have Array
Jolt Spec for Json that may or may not have Array

Time:10-22

Any help is greatly Appreciated.

I Have input JSON that can have Phone in array or it can be blank or it can be missing.

[
  {
    "Name": "abc",
    "Phone": [
      {
        "office-1": "123",
        "home-1": "989"
      },
      {
        "office-1": "456",
        "home-1": "999"
      }
    ],
    "Email": "[email protected]"
  },
  {
    "Name": "efg",
    "Phone": [],
    "Email": "[email protected]"
  },
  {
    "Name": "xyz",
    "Email": "[email protected]"
  }
]

My Jolt is already able to convert the Phone number array, but it is not working if the label Phone is missing in JSON input.

Expected output:

[
  {
    "Name": "abc",
    "office-1": "123",
    "home-1": "989",
    "Email": "[email protected]"
  },
  {
    "Name": "abc",
    "office-1": "456",
    "home-1": "999",
    "Email": "[email protected]"
  },
  {
    "Name": "efg",
    "Email": "[email protected]"
  },
  {
    "Name": "xyz",
    "Email": "[email protected]"
  }
]

Please help

CodePudding user response:

You can walk through the objects after separating the stuff under Phone node and the others such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Phone": {
          "*": {
            "*": "&3.&1.&",
            "@(2,Name)": "&3.&1.Name",
            "@(2,Email)": "&3.&1.Email"
          }
        },
        "*": "&1.&1.&" // "else" case
      }
    }
  },
  {
   // get rid of object labels
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  },
  {
   // get rid of duplicated values of some attributes
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  }
]

the demo on the site enter image description here

CodePudding user response:

new sample of data where I have kept non array data in between array data

  • Related