Home > Net >  Remove null values from JSON output using Jolt
Remove null values from JSON output using Jolt

Time:12-17

Can you suggest me a way for removing the null value from the json output as describe below:

Input

{
  "userId": "1",
  "age": "20",
  "desc1": "value desc1",
  "desc2": "value desc2",
  "desc3": "value desc3",
  "desc4": "value desc4",
  "desc5": "value desc5",
  "desc6": "value desc6",
  "desc7": "value desc7"
}

Spec

[
  {
    "operation": "shift",
    "spec": {
      "desc4": "test4",
      "desc5": "test5",
      "desc6": "test6",
      "desc1|desc2|desc3": {
        "$": "additionalInformationList[#2].typeCode",
        "@": "additionalInformationList[#2].value"
      }
    }
  }
]

Output

{
  "test4": "value desc4",
  "test5": "value desc5",
  "test6": "value desc6",
  "additionalInformationList": [null,null,null,
    {
      "typeCode": "desc1",
      "value": "value desc1"
    },
    {
      "typeCode": "desc2",
      "value": "value desc2"
    },
    {
      "typeCode": "desc3",
      "value": "value desc3"
    }
  ]
}

Any suggestion how to remove the null values please ?

CodePudding user response:

Indeed, it's enough to use

[
  {
    "operation": "shift",
    "spec": {
      "desc*": "&",
      "desc1|desc2|desc3": {
        "$": "additionalInformationList[#2].typeCode",
        "@": "additionalInformationList[#2].value"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "desc7": ""
    }
  }
]

if you won't rename the desc attributes with test in order to get

{
  "additionalInformationList" : [ {
    "typeCode" : "desc1",
    "value" : "value desc1"
  }, {
    "typeCode" : "desc2",
    "value" : "value desc2"
  }, {
    "typeCode" : "desc3",
    "value" : "value desc3"
  } ],
  "desc4" : "value desc4",
  "desc5" : "value desc5",
  "desc6" : "value desc6"
}

For the current case, you can use

[
  {
    "operation": "shift",
    "spec": {
      "desc*": "test&",
      "desc1|desc2|desc3": {
        "$": "additionalInformationList[#2].typeCode",
        "@": "additionalInformationList[#2].value"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "test*": {
        "$": "@(0)"
      },
      "*": "&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*desc*": "=split('desc',@(1,&))"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*desc*": "=join('',@(1,&))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*desc*": {
        "$": "@(0)"
      },
      "*": "&"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "test7": ""
    }
  }
]

enter image description here

or shorter case by individually writing the key-value pairs of desc4, desc5 and desc6 attributes might be

[
  {
    "operation": "shift",
    "spec": {
      "desc4": "test4",
      "desc5": "test5",
      "desc6": "test6",
      "desc*": {
        "$": "&.code",
        "@": "&.value"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "desc7": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "test*": "&",
      "*": "additionalInformation[]"
    }
  }
]

enter image description here

  • Related