I am trying to filter through an API (JSON format). I am pretty new to this and have no idea where to start. For simplicity’s sake, I will only give part of the API output I would technically get. I am using Python 3, specifically Python 3.9.2.
{ "success": true, "currency": "CAD", "timestamp": 1615516661, "items_list": { "AK-47 | Aquamarine Revenge (Minimal Wear)": { "name": "AK-47 | Aquamarine Revenge (Minimal Wear)", "marketable": 1, "tradable": 1, "classid": "1819967861", "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWdY781lxLuW8Njw31Dn8xc_YTqmJ4DDJFM2ZwqE_ATtx-u7g8C5vpjOzHM263E8pSGKJ1XuG9M", "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWdY781lteXA54vwxgbjrkc5ZD3wLNLGcFVrYl6GrAS2x7y7g8PvupidzyRmuCUl4H6IzkSpwUYbC6zHyEM", "type": "Weapon", "weapon_type": "Rifle", "gun_type": "AK-47", "exterior": "Minimal Wear", "rarity": "Covert", "rarity_color": "eb4b4b", "price": { "24_hours": { "average": 65.24, "median": 64.41, "sold": "46", "standard_deviation": "4.43", "lowest_price": 61.49, "highest_price": 75.76 }, "7_days": { "average": 64.7, "median": 64.49, "sold": "240", "standard_deviation": "4.32", "lowest_price": 53.83, "highest_price": 75.76 }, "30_days": { "average": 59.39, "median": 59.3, "sold": "982", "standard_deviation": "8.84", "lowest_price": 0.18, "highest_price": 75.76 }, "all_time": { "average": 38.1, "median": 40.48, "sold": "169635", "standard_deviation": "38.77", "lowest_price": 0.18, "highest_price": 125.91 } }, "first_sale_date": "1432764000" }, "AK-47 | Aquamarine Revenge (Well-Worn)": { "name": "AK-47 | Aquamarine Revenge (Well-Worn)", "marketable": 1, "tradable": 1, "classid": "2095030666", "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWZU7Mxkh6eU896n0FXk-RJsNzv3cI-WJAA3YFDTqFa2l-u6jJW4uJqdyCBluyEm-z-DyCua9lLK", "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWZU7Mxkh9bN9J7yjRrhrUFuazjzJteVJlQ6NVHTrFe3wObs15G06picwHFnvid25C3bnhSzn1gSOQz0szG-", "type": "Weapon", "weapon_type": "Rifle", "gun_type": "AK-47", "exterior": "Well-Worn", "rarity": "Covert", "rarity_color": "eb4b4b", "price": { "24_hours": { "average": 34.31, "median": 34.14, "sold": "40", "standard_deviation": "3.67", "lowest_price": 31.93, "highest_price": 37.23 }, "7_days": { "average": 35.72, "median": 35.8, "sold": "382", "standard_deviation": "5.07", "lowest_price": 29.7, "highest_price": 38.93 }, "30_days": { "average": 33.14, "median": 33.11, "sold": "1632", "standard_deviation": "7", "lowest_price": 26.77, "highest_price": 38.93 }, "all_time": { "average": 21.89, "median": 23.76, "sold": "212874", "standard_deviation": "35.16", "lowest_price": 11.78, "highest_price": 71.85 } }, "first_sale_date": "1432764000" } } }
The output I would like is something like the following:
{ "success": true, "currency": "CAD", "timestamp": 1615516661, "items_list": { "AK-47 | Aquamarine Revenge (Minimal Wear)": { "name": "AK-47 | Aquamarine Revenge (Minimal Wear)", "type": "Weapon", "exterior": "Minimal Wear", "rarity": "Covert", "price": { "24_hours": { "average": 65.24, "median": 64.41, "sold": "46", "standard_deviation": "4.43", "lowest_price": 61.49, "highest_price": 75.76 }, "7_days": { "average": 64.7, "median": 64.49, "sold": "240", "standard_deviation": "4.32", "lowest_price": 53.83, "highest_price": 75.76 }, "30_days": { "average": 59.39, "median": 59.3, "sold": "982", "standard_deviation": "8.84", "lowest_price": 0.18, "highest_price": 75.76 }, "all_time": { "average": 38.1, "median": 40.48, "sold": "169635", "standard_deviation": "38.77", "lowest_price": 0.18, "highest_price": 125.91 } }, }, "AK-47 | Aquamarine Revenge (Well-Worn)": { "name": "AK-47 | Aquamarine Revenge (Well-Worn)", "type": "Weapon", "exterior": "Well-Worn", "rarity": "Covert", "price": { "24_hours": { "average": 34.31, "median": 34.14, "sold": "40", "standard_deviation": "3.67", "lowest_price": 31.93, "highest_price": 37.23 }, "7_days": { "average": 35.72, "median": 35.8, "sold": "382", "standard_deviation": "5.07", "lowest_price": 29.7, "highest_price": 38.93 }, "30_days": { "average": 33.14, "median": 33.11, "sold": "1632", "standard_deviation": "7", "lowest_price": 26.77, "highest_price": 38.93 }, "all_time": { "average": 21.89, "median": 23.76, "sold": "212874", "standard_deviation": "35.16", "lowest_price": 11.78, "highest_price": 71.85 } }, } } }
As you can see, I am removing some of the contents. How can I accomplish this? Any pointers would be appreciated. Thanks in advance!
Advertisement
Answer
Let’s say your file with json data is named api_data.json.
import json # convert json data to a python dict with open('api_data.json') as f: data = json.load(f) # data is a python dict # delete unwanted keys and associated data del data['items_list']['AK-47 | Aquamarine Revenge (Minimal Wear)']['marketable'] del data['items_list']['AK-47 | Aquamarine Revenge (Minimal Wear)']['tradable'] # Now, save back as json file with open('api_data.json') as f: json.dump(data, f)