Skip to content Skip to sidebar Skip to footer

Filter Json File With Python

How to filter a json file to show only the information I need? To start off I want to say I'm fairly new to python and working with JSON so sorry if this question was asked before

Solution 1:

Let's say we can use dict comprehension, then it will be

output_dict = [{k:v for k,v in x.items() if k in ["Item", "Price"]} for x in input_dict]

Solution 2:

You can also do it like this :)

>>> [{key: d[key]forkeyin['Item', 'Price']} fordininput_dict] # youshouldrenameitto `input_list` ratherthan `input_dict` :)
[{'Item': 10, 'Price': 8.9}, {'Item': 15, 'Price': 2.6}]

Solution 3:

import pprint
withopen('data.json', 'r') as f:
        qe = json.load(f)
        list = []
        for item in qe['<your data>']: 
            query = (f'{item["Item"]}{item["Price"]}')

        print("query")

Post a Comment for "Filter Json File With Python"