Search code examples
jsonjolt

Jolt Spec to remove null elements from the nested array


Please help in removing nested null elements at root level and inside the options array and also in understanding why options array has null elements in it.

Input:

[
  {
    "autoNumber": false,
    "byteLength": 18,
    "controllerName": null,
    "createable": true,
    "defaultValue": null,
    "label": "Case ID",
    "name": "Id",
    "nillable": false,
    "picklistValues": []
  },
  {
    "autoNumber": false,
    "byteLength": 18,
    "createable": false,
    "label": "Asset ID",
    "name": "AssetId",
    "picklistValues": [],
    "nillable": false,
    "type": "reference"
  },
  {
    "autoNumber": false,
    "byteLength": 765,
    "createable": true,
    "label": "Case Type",
    "length": 255,
    "name": "Select list",
    "nillable": true,
    "picklistValues": [
      {
        "label": "Mechanical",
        "validFor": null,
        "value": "Mechanical"
      },
      {
        "label": "Electrical",
        "validFor": null,
        "value": "Electrical"
      }
    ],
    "type": "picklist"
  }
]

Jolt-Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "createable": {
          "true": {
            "@(2,name)": ["[#4].crm_field_identifier", "[#4].target_property"],
            "@(2,nillable)": {
              "true": {
                "#false": "[#6].required"
              },
              "false": {
                "#true": "[#6].required"
              }
            },
            "@(2,picklistValues)": {
              "*": {
                "label": "[#6].options[#2].name",
                "value": "[#6].options[#2].value"
              }
            },
            "@(2,defaultValue)": "[&3].default_value"
          }
        }
      }
    }
  }
]

Current O/P:

[
  {
    "crm_field_identifier": "Id",
    "target_property": "Id",
    "required": "true",
    "default_value": null
  },
  null,
  {
    "crm_field_identifier": "Select list",
    "target_property": "Select list",
    "required": "false",
    "options": [
      null,
      null,
      {
        "name": "Mechanical",
        "value": "Mechanical"
      },
      {
        "name": "Electrical",
        "value": "Electrical"
      }
    ],
    "default_value": null
  }
]

Required O/P:

[
  {
    "crm_field_identifier": "Id",
    "target_property": "Id",
    "required": "true",
    "default_value": null
  },
  {
    "crm_field_identifier": "Select list",
    "required": "false",
    "options": [
      {
        "name": "Mechanical",
        "value": "Mechanical"
      },
      {
        "name": "Electrical",
        "value": "Electrical"
      }
    ],
    "default_value": null
  }
]

Already checked:

  1. Modify spec with recursivelySquashNulls which didn't work

  2. Remove spec

  3. Existing Stackoverflow questions


Solution

  • You can get rid of square bracket symbols which wrap #-prefixed integers, while decrementing those integers except for the array(namely "options"),

    eg. convert [#6] to &5, [#4] to &3; for "options" : [#2] to [&1](keep square brackets) such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "createable": {
              "true": {
                "@(2,name)": ["&3.crm_field_identifier", "&3.target_property"],
                "@(2,nillable)": {
                  "true": {
                    "#false": "&5.required"
                  },
                  "false": {
                    "#true": "&5.required"
                  }
                },
                "@(2,picklistValues)": {
                  "*": {
                    "label": "&5.options[&1].name",
                    "value": "&5.options[&1].value"
                  }
                },
                "@(2,defaultValue)": "&3.default_value"
              }
            }
          }
        }
      },
      { // get rid of the integer object keys
        "operation": "shift",
        "spec": {
          "*": {
            "@": ""
          }
        }
      }
    ]
    

    keep in mind that #-prefixed identifier starts counting from 1, while &-prefixed from 0

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

    enter image description here