BBG_GIS
BBG_GIS

Reputation: 305

How do i filter nested json file using python?

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?

Upvotes: 1

Views: 8931

Answers (2)

Ashutosh Narayan Jha
Ashutosh Narayan Jha

Reputation: 69

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.. 

Upvotes: 1

j1-lee
j1-lee

Reputation: 13929

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"
  }
]

Upvotes: 4

Related Questions