Deleting specific JSON lines while iterating thorugh key in Python

Question:

I have a large JSON file that contains image annotation data. I am iterating through one of the keys below.:

import json
  
# Opening JSON file
f = open('annotations.json')
  
# returns JSON object as 
# a dictionary
data = json.load(f)
  
# Iterating through the json
# list
for i in data['annotations']:
    if i['segmentation'] == [[]]:
        print(i['segmentation'])
        del i
    #print(i['segmentation'])
  
# Closing file
f.close()

Printing the returned dictionaries, they look like this:

{"iscrowd":0,"image_id":32,"bbox":[],"segmentation":[[]],"category_id":2,"id":339,"area":0}

I am trying to remove the following above lines in the annotations key that contain no data for segmentation. I am able to extract these lines, I am just not sure how to remove them without breaking the format of the file.

{"iscrowd":0,"image_id":32,"bbox":[],"segmentation":[[]],"category_id":2,"id":339,"area":0}
,{"iscrowd":0,"image_id":32,"bbox":[],"segmentation":[[]],"category_id":2,"id":340,"area":0}
,{"iscrowd":0,"image_id":32,"bbox":[],"segmentation":[[]],"category_id":2,"id":341,"area":0}
,{"iscrowd":0,"image_id":32,"bbox":[],"segmentation":[[]],"category_id":2,"id":342,"area":0},
...

Here is what finally got it working for me:

import json
  
# Opening JSON file
f = open('annotations.json')
  
# returns JSON object as 
# a dictionary
data = json.load(f)

# Closing file
f.close()

# Iterating through the json
# list
count = 0
for key in data['annotations']:
    count +=1
    if key['segmentation'] == [[]]:
        print(key['segmentation'])
        data["annotations"].pop(count)
    if key['bbox'] == []:
        data["annotations"].pop(count)
    #print(i['segmentation'])

with open("newannotations.json", "w") as json_file:  
        json.dump(data, json_file)
Asked By: devman3211

||

Answers:

The function json.loads() returns a python dictionary, which you can then modify as you’d like. Similarly json.dumps() can be used to write a json file from a python dictionary.

In order to remove an entry from a dictionary, you can use the dictionary pop() method. Assuming in the above you want to delete each entry referred to with the key i (as per the del i) if the entry in data["annotations"][i]["segmentation"] ==[[]], one could do it approximately as follows:

import json
  
# Opening JSON file
f = open('annotations.json')
  
# returns JSON object as 
# a dictionary
data = json.load(f)

# Closing file
f.close()

# Iterating through the json
# list
for key in data['annotations']:
    if data["annotations"][key]['segmentation'] == [[]]:
        print(data["annotations"][key]['segmentation'])
        data["annotations"].pop(key)
    #print(i['segmentation'])

with open("newannotations.json", "w") as json_file:  
        json.dump(data, json_file)

Is this what you wanted to do?

Answered By: Otto Hanski
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.