Search code examples
jsonjolt

Prevent/remove null values in array as result of JOLT transform


I have a large payload for a complex (for me :-) JOLT transformation spec that gives empty/null values in the end result. Since the input, spec and output are quite big I have provided pastebin links. Hope this is OK.

Input is: input

JOLT transform spec is: JOLT spec

Result is: output

Part that is not as we want is the pim_network part as shown below (the 2 empty asset_id ones, they are caused by null values after the 4th shift operation):

  "pim_network": [
    {
      "ifindex": 1,
      "ip_address": "127.0.0.1",
      "ip_subnet": 8,
      "description": "lo",
      "mac_address": "00:00:00:00:00:00",
      "asset_id": ""
    },
    {
      "asset_id": ""
    },
    {
      "ifindex": 2,
      "ip_address": "192.168.119.129",
      "ip_subnet": 24,
      "description": "ens33",
      "mac_address": "00:0c:29:02:b8:cd",
      "asset_id": ""
    },
    {
      "asset_id": ""
    },
    {
      "ifindex": 3,
      "ip_address": "172.17.0.1",
      "ip_subnet": 16,
      "description": "docker0",
      "mac_address": "02:42:36:ae:50:55",
      "asset_id": ""
    },
    {
      "ifindex": 4,
      "ip_address": "172.18.0.1",
      "ip_subnet": 16,
      "description": "br-c75ac7c3061d",
      "mac_address": "02:42:aa:97:1e:97",
      "asset_id": ""
    }
  ],

Expected result is that we only have the 4 valid IPv4 entries in the overall result. Hope somebody can assist.

P.S. I know JSON is not 100% properly visually formatted. Unfortunatelty this is what we get as output from the JOLT demo site.


Solution

    • As much as I can observe, the usage of modify-default-beta is redundant. The pair "asset_id" : "" won't be generated if this spec is removed
    • The identifier [#2] should be replaced by &3 whenever the attributes under the pim_network node are picked. The [#2] should be applied within the upcoming spec

    As a result, you might shorten the transformation through use of the folowing one

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&",
            "pim_network": {
              "*": {
                "*": "&",
                "addresses": {
                  "*": {
                    "*": "&4.&1.others.&",
                    "addr_info": {
                      "*": {
                        "*": "&6.&3.&2[&1].&"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "pim_network": {
            "*": {
              "addr_info": {
                "*": {
                  "@2,others": { "*": "&5.&4_&2.&" },
                  "*": "&4.&3_&1.&"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "pim_network": {
            "*": {
              "family": {
                "inet": {
                  "@2,ifindex": "&4.&3.ifindex",
                  "@2,local": "&4.&3.ip_address",
                  "@2,prefixlen": "&4.&3.ip_subnet",
                  "@2,label": "&4.&3.description",
                  "@2,address": "&4.&3.mac_address"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "pim_hostname": {
            "SHORT_HOSTNAME": "hostname"
          },
          "pim_distro": {
            "PRETTY_NAME": "os_type"
          },
          "pim_hardware": {
            "processor_count": "cpu_count",
            "hw_memory": "memory"
          },
          "pim_storage": {
            "*": {
              "NAME": "&2[&1].drive_name",
              "SIZE": "&2[&1].capacity",
              "TYPE": "&2[&1].type_enum"
            }
          },
          "pim_network": {
            "*": {
              "*": "&2[#2].&"
            }
          },
          "pim_software": {
            "*": {
              "Name": "&2[&1].name",
              "Version": "&2[&1].version"
            }
          },
          "pim_services": {
            "*": {
              "Id": "&2[&1].&",
              "Names": "&2[&1].name",
              "Description": "&2[&1].friendly_name",
              "LoadState": "&2[&1].loadstate",
              "ActiveState": "&2[&1].state",
              "UnitFileState": "&2[&1].unitfilestate",
              "UnitFilePreset": "&2[&1].start_mode"
            }
          }
        }
      }
    ]