This is a json file containing several category. What I am trying to achieve is that if the category is traffic sign how to remove the entire section for all category: traffic sign.I want to remove this selected portion for all category=traffic sign
I tried several methods but I couldn’t sort it out, because I am new to json and its structure.
2
Answers
import json
with open(‘test.json’, ‘r’) as jf:
jsonFile = json.load(jf)
print(‘Length of JSON object before cleaning: ‘, len(jsonFile.keys()))
testJson = {}
keyList = jsonFile.keys()
for key in keyList:
if not key.startswith(‘traffic’):
print(key)
testJson[key] = jsonFile[key]
print(‘Length of JSON object after cleaning: ‘, len(testJson.keys()))
with open(‘test.json’, ‘w’) as jf:
json.dump(testJson, jf)
The area you selected is actually an array. We can just use .filter() on it. I can’t see how the JSON file is laid out with the limited view given in the image but assuming that you know how to drill down into the document to the "labels" section I can take it from there.
The question is tagged as JavaScript but I see a python file in the screenshot. I’m a little confused on what language you want this in. But I’ll assume that that JavaScript tag is correct. That line above will filter the array and return a new array with the undesirable stuff removed. Try the code snippit, notice how the item that is "category":"traffic sign" is missing from the output. Now all you have to do is figure out how to drill into the JSON file to get to "labels" and do some file input and output.