1

I'm trying to filter a nested JSON file. I want to create a new json file with fitering "classes" and "id". The source JSON file :

[
{"data": {"id": "ed", "label": "Executive Director (Harriet)"},
 "classes": "purple"  
 },

{"data": {"id": "vp1", "label": "Vice President (Sarah)"},
 "classes": "square"  
 },

{"data": {"id": "vp2", "label": "Vice President (Charlotte)"},
 "classes": "square"  
 },

{"data": {"id": "po1", "label": "Program Officer (Sojourner)"},
 "classes": "green diamond"  
 },

 {"data": {"id": "po2", "label": "Program Officer (Sojourner)"},
 "classes": "green diamond"  
 },
{"data": {"id": "pa", "label": "Program Associate (Ellen)"},
 "classes": "myimage"  
 }
 ]

My goal is filter 'classes': 'green diamond' having 'id': 'po1' . Then remove all classes with 'green diamond', except 'id': 'po1'.

The result:

[
{"data": {"id": "ed", "label": "Executive Director (Harriet)"},
 "classes": "purple"  
 },

{"data": {"id": "vp1", "label": "Vice President (Sarah)"},
 "classes": "square"  
 },

{"data": {"id": "vp2", "label": "Vice President (Charlotte)"},
 "classes": "square"  
 },

{"data": {"id": "po1", "label": "Program Officer (Sojourner)"},
 "classes": "green diamond"  
 },


{"data": {"id": "pa", "label": "Program Associate (Ellen)"},
 "classes": "myimage"  
 }]

I tried to used this code to fetch the data but it raised an error:

import json

# Loding the data
with open("D:/bb.json", 'r') as file:
    content = file.read()

# Converting json_data to python dictionary format
json_data = json.loads(content)

quantite = -1  # -1 for not found case
for data in json_data[0]:

    # Checking for specific pair
    if data['classes'] == 'square' and data['id'] == 'vp2':
        print(data)
        break

How can i filter such a json file?

1
  • if you are going to write json use the json.dumps and do something like json.dumps([ x for x in json_data if x['classes'] == 'square' and x['id'] == 'vp2']) Commented May 24, 2021 at 18:52

2 Answers 2

4

As the loaded json data is just nested lists and dicts, you can use the ordinary list/dict operations; in particular, list comprehension is useful.

import json

with open('input', 'r') as f:
    data = json.loads(f.read())

filtered = [x for x in data if not (x['classes'] == 'green diamond' and x['data']['id'] != 'po1')]

with open('output', 'w') as f:
    f.write(json.dumps(filtered, indent=2))

Result:

[
  {
    "data": {
      "id": "ed",
      "label": "Executive Director (Harriet)"
    },
    "classes": "purple"
  },
  {
    "data": {
      "id": "vp1",
      "label": "Vice President (Sarah)"
    },
    "classes": "square"
  },
  {
    "data": {
      "id": "vp2",
      "label": "Vice President (Charlotte)"
    },
    "classes": "square"
  },
  {
    "data": {
      "id": "po1",
      "label": "Program Officer (Sojourner)"
    },
    "classes": "green diamond"
  },
  {
    "data": {
      "id": "pa",
      "label": "Program Associate (Ellen)"
    },
    "classes": "myimage"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

1

Try following, did a bit update to your code

import json
# Loding the data
pathToFile = "bb.json"
with open(pathToFile, 'r') as file:
    content = file.read()
# Converting json_data to python dictionary format
json_data = json.loads(content)

removeEntryClass = "green diamond"
keepId = "po1"
outputList = []

for entry in json_data:
    if entry["classes"] == removeEntryClass and entry["data"]['id'] != keepId :
        continue
    outputList.append(entry)

print(outputList)
#You can save this list into jour file.. 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.