raja
raja

Reputation: 61

Traversing a json to remove nulls fields using jq

I have a JSON as follows but need a way to remove nulls before putting it into elasticsearch. Looking for a simple jq command to remove nulls that i can incorporate into my bash script unless there's a way to do this in elasticsearch

{
    "master_no": {
      "master_no": 100000000,
      "barcode": "E00000000",
      "external_key": null,
      "umid": null
    },
    "cust_id": {
      "other_cust_id": null,
      "cust_reference": null,
      "external_key": null,
      "list_id": null,
      "cust_id": null
    },
    "customer_name": null,
    "master_desc": "test Custom Patch - test",
    "barcode": "E00000000",
    "container_master_no": null,
    "master_status": "I",
    "length": "0:00",
    "format_no": {
      "format_desc": null,
      "external_key": null,
      "format_no": null
    },
    "lib_master_audio": [
      {
        "master_no": 10000000,
        "audio_channel_no": {
          "audio_channel_no": 10,
          "audio_channel": "1",
          "external_key": null
        }
      },
      {
        "master_no": 100000000,
        "audio_channel_no": {
          "audio_channel_no": 10,
          "audio_channel": "2",
          "external_key": null
        }
      }
    ]
}

Thanks

Upvotes: 0

Views: 382

Answers (1)

Amit
Amit

Reputation: 32376

This GitHub issue on remove null key and values from JSON can help you, in short, some handful of command might help you like, mentioned in this link :

del(.[][] | nulls)

Please note there are several methods of doing this, please check which one works for you.

As pointed out in comments by @oguz, Please use https://github.com/stedolan/jq/issues/104#issuecomment-289637207 which work with the latest version.

Upvotes: 1

Related Questions