Home > database >  Python - remove json object if missing key
Python - remove json object if missing key

Time:02-20

I'm looking to delete all objects from some JSON data if it is missing the key or value.

Here is a snippet of the JSON data:

[
  {
    "cacheId": "eO8MDieauGIJ",
    "pagemap": {}
  },
  {
    "cacheId": "AWYYu9XQnuwJ",
    "pagemap": {
      "cse_image": [
        {
          "src": "https://someimage.png"
        }
       ]
      }
   },
  {
    "cacheId": "AWYYu9XQnuwJ",
    "pagemap": {
      "cse_image": [
        {
        }
       ]
      }
   }
]

I'm looking to delete the 1st and 3rd object in the data because the 1st object has an empty ['pagemap'] and the 3rd object has an empty ['pagemap']['cse_image']

Here is what I've tried so far without any luck:

for result in data[:]:
    if result['pagemap']['cse_image'] == []:
        data.remove(result)

for result in data[:]:
    if result['pagemap'] == None:
        data.remove(result)

for result in data[:]:
    if len(result['pagemap']) == 0:
        data.remove(result)

Thanks for the help!

CodePudding user response:

Two things:

  1. You don't want to remove elements from a list while iterating over them -- the memory you're removing is shifting as you're accessing it.
  2. The third object has a nonempty ['pagemap']['cse_image']. Its value is a one-element list containing an empty dictionary. You need to index into the list to check whether or not the inner dictionary is empty.

With these two points in mind, here is an approach using filter() that also leverages the fact that empty data structures have falsy values:

result = list(filter(lambda x: x['pagemap'] and x['pagemap']['cse_image'] and x['pagemap']['cse_image'][0], data))

print(result)

CodePudding user response:

If that data structure remains consistent, you can do it with a list comprehension.

[e for e in d if e["pagemap"] and e["pagemap"]["cse_image"] and any(e["pagemap"]["cse_image"])]

produces:

[{'cacheId': 'AWYYu9XQnuwJ', 'pagemap': {'cse_image': [{'src': 'https://someimage.png'}]}}]

Where d is your given data structure.

  • Related