Search code examples
jsonjolt

Remove empty object from JSON


Source JSON:

{
  "id": 3,
  "status": "active",
  "media": [
    {
      "content": {
        "logo": {
          "id": 24808
        }
      },
      "web": {
        "primary": {
          "id": 909
        }
      },
      "description": {},
      "status": "blocked"
    }
  ]
}

Field description can be empty {}, but sometimes not. How can I remove object description only if it's an empty? Or for example web can be empty too. So main goal - remove empty objects from media array.

{
  "id": 3,
  "status": "active",
  "media": [
    {
      "content": {
        "logo": {
          "id": 24808
        }
      },
      "web": {
        "primary": {
          "id": 909
        }
      },
      "status": "blocked"
    }
  ]
}

UPDATE

Today I noticed some problem. When inside content, web or description multiple objects like in this example:

{
   "id":3,
   "status":"active",
   "media":[
      {
         "content":{
            "logo":{
               "id":24808
            },
            "main_theme":{
               "id":91390
            },
            "preview_pic":{
               "id":11
            }
         },
         "web":{
            "primary":{
               "id":909
            },
            "outer_link":{
               "id":501
            }
         },
         "description":{
            
         },
         "status":"blocked"
      }
   ]
}

Script creates duplicates: dups


Solution

  • You can use the following shift transformation spec :

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "media": {
            "*": {
              "*": {
                "*": {
                  "@1": "&4[&3].&2"
                }
              }
            }
          }
        }
      }
    ]
    

    this way, we're trying to go deeper in order to get rid of the respective counterpart value which will be a null for the "description" attribute in this case.

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

    enter image description here

    Edit : the following one will be a more generic alternative considering the case you have encountered lately :

    [
      { // eliminate the attributes with {} on the RHS 
        "operation": "shift",
        "spec": {
          "*": "&",
          "media": {
            "*": {
              "*": {
                "*": "&3[&2].&1.&"
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "media": {
            "*": {
              "*": {
                "*": ["=notNull", "NulL"]
              }
            }
          }
        }
      },
      { // keep the non-nested attributes similar to "status"
        "operation": "shift",
        "spec": {
          "*": "&",
          "media": {
            "*": {
              "*": {
                "*": {
                  "NulL": {
                    "$1": "&5[&4].&3"
                  },
                  "*": "&4[&3].&2.&1.&"
                }
              }
            }
          }
        }
      }
    ]