Search code examples
jsonjolt

Remove keys that contains empty string


I receive some JSON's from REST API.

[
  {
    "id": 58939956,
    "campaign_id": 1012995497,
    "status": 0,
    "start_time": "",
    "goal_type": ""
  },
  {
    "id": 58939957,
    "campaign_id": 1012995497,
    "status": 0,
    "start_time": "2023-11-01",
    "goal_type": "cpc"
  }
]

If property is not provided (key in JSON) API returns just empty string . I want to filter this JSON and get rid of these keys (but only if it's empty for certain object). From input above I want to achive this output (keys naming can be different):

[
  {
    "id": 58939956,
    "campaign_id": 1012995497,
    "status": 0
  },
  {
    "id": 58939957,
    "campaign_id": 1012995497,
    "status": 0,
    "start_time": "2023-11-01",
    "goal_type": "cpc"
  }
]

Solution

  • You can use "" notation for keys to be removed within a shift transformation such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": { "@1": "[&3].&2" }, // the elements with values other than ""
              "": { "": "[&3]" }
            }
          }
        }
      }
    ]
    

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

    enter image description here