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.
JavaScript
x
106
106
1
{
2
"success": true,
3
"currency": "CAD",
4
"timestamp": 1615516661,
5
"items_list": {
6
"AK-47 | Aquamarine Revenge (Minimal Wear)": {
7
"name": "AK-47 | Aquamarine Revenge (Minimal Wear)",
8
"marketable": 1,
9
"tradable": 1,
10
"classid": "1819967861",
11
"icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWdY781lxLuW8Njw31Dn8xc_YTqmJ4DDJFM2ZwqE_ATtx-u7g8C5vpjOzHM263E8pSGKJ1XuG9M",
12
"icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWdY781lteXA54vwxgbjrkc5ZD3wLNLGcFVrYl6GrAS2x7y7g8PvupidzyRmuCUl4H6IzkSpwUYbC6zHyEM",
13
"type": "Weapon",
14
"weapon_type": "Rifle",
15
"gun_type": "AK-47",
16
"exterior": "Minimal Wear",
17
"rarity": "Covert",
18
"rarity_color": "eb4b4b",
19
"price": {
20
"24_hours": {
21
"average": 65.24,
22
"median": 64.41,
23
"sold": "46",
24
"standard_deviation": "4.43",
25
"lowest_price": 61.49,
26
"highest_price": 75.76
27
},
28
"7_days": {
29
"average": 64.7,
30
"median": 64.49,
31
"sold": "240",
32
"standard_deviation": "4.32",
33
"lowest_price": 53.83,
34
"highest_price": 75.76
35
},
36
"30_days": {
37
"average": 59.39,
38
"median": 59.3,
39
"sold": "982",
40
"standard_deviation": "8.84",
41
"lowest_price": 0.18,
42
"highest_price": 75.76
43
},
44
"all_time": {
45
"average": 38.1,
46
"median": 40.48,
47
"sold": "169635",
48
"standard_deviation": "38.77",
49
"lowest_price": 0.18,
50
"highest_price": 125.91
51
}
52
},
53
"first_sale_date": "1432764000"
54
},
55
"AK-47 | Aquamarine Revenge (Well-Worn)": {
56
"name": "AK-47 | Aquamarine Revenge (Well-Worn)",
57
"marketable": 1,
58
"tradable": 1,
59
"classid": "2095030666",
60
"icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWZU7Mxkh6eU896n0FXk-RJsNzv3cI-WJAA3YFDTqFa2l-u6jJW4uJqdyCBluyEm-z-DyCua9lLK",
61
"icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5gZKKkPLLMrfFqWZU7Mxkh9bN9J7yjRrhrUFuazjzJteVJlQ6NVHTrFe3wObs15G06picwHFnvid25C3bnhSzn1gSOQz0szG-",
62
"type": "Weapon",
63
"weapon_type": "Rifle",
64
"gun_type": "AK-47",
65
"exterior": "Well-Worn",
66
"rarity": "Covert",
67
"rarity_color": "eb4b4b",
68
"price": {
69
"24_hours": {
70
"average": 34.31,
71
"median": 34.14,
72
"sold": "40",
73
"standard_deviation": "3.67",
74
"lowest_price": 31.93,
75
"highest_price": 37.23
76
},
77
"7_days": {
78
"average": 35.72,
79
"median": 35.8,
80
"sold": "382",
81
"standard_deviation": "5.07",
82
"lowest_price": 29.7,
83
"highest_price": 38.93
84
},
85
"30_days": {
86
"average": 33.14,
87
"median": 33.11,
88
"sold": "1632",
89
"standard_deviation": "7",
90
"lowest_price": 26.77,
91
"highest_price": 38.93
92
},
93
"all_time": {
94
"average": 21.89,
95
"median": 23.76,
96
"sold": "212874",
97
"standard_deviation": "35.16",
98
"lowest_price": 11.78,
99
"highest_price": 71.85
100
}
101
},
102
"first_sale_date": "1432764000"
103
}
104
}
105
}
106
The output I would like is something like the following:
JavaScript
1
88
88
1
{
2
"success": true,
3
"currency": "CAD",
4
"timestamp": 1615516661,
5
"items_list": {
6
"AK-47 | Aquamarine Revenge (Minimal Wear)": {
7
"name": "AK-47 | Aquamarine Revenge (Minimal Wear)",
8
"type": "Weapon",
9
"exterior": "Minimal Wear",
10
"rarity": "Covert",
11
"price": {
12
"24_hours": {
13
"average": 65.24,
14
"median": 64.41,
15
"sold": "46",
16
"standard_deviation": "4.43",
17
"lowest_price": 61.49,
18
"highest_price": 75.76
19
},
20
"7_days": {
21
"average": 64.7,
22
"median": 64.49,
23
"sold": "240",
24
"standard_deviation": "4.32",
25
"lowest_price": 53.83,
26
"highest_price": 75.76
27
},
28
"30_days": {
29
"average": 59.39,
30
"median": 59.3,
31
"sold": "982",
32
"standard_deviation": "8.84",
33
"lowest_price": 0.18,
34
"highest_price": 75.76
35
},
36
"all_time": {
37
"average": 38.1,
38
"median": 40.48,
39
"sold": "169635",
40
"standard_deviation": "38.77",
41
"lowest_price": 0.18,
42
"highest_price": 125.91
43
}
44
},
45
},
46
"AK-47 | Aquamarine Revenge (Well-Worn)": {
47
"name": "AK-47 | Aquamarine Revenge (Well-Worn)",
48
"type": "Weapon",
49
"exterior": "Well-Worn",
50
"rarity": "Covert",
51
"price": {
52
"24_hours": {
53
"average": 34.31,
54
"median": 34.14,
55
"sold": "40",
56
"standard_deviation": "3.67",
57
"lowest_price": 31.93,
58
"highest_price": 37.23
59
},
60
"7_days": {
61
"average": 35.72,
62
"median": 35.8,
63
"sold": "382",
64
"standard_deviation": "5.07",
65
"lowest_price": 29.7,
66
"highest_price": 38.93
67
},
68
"30_days": {
69
"average": 33.14,
70
"median": 33.11,
71
"sold": "1632",
72
"standard_deviation": "7",
73
"lowest_price": 26.77,
74
"highest_price": 38.93
75
},
76
"all_time": {
77
"average": 21.89,
78
"median": 23.76,
79
"sold": "212874",
80
"standard_deviation": "35.16",
81
"lowest_price": 11.78,
82
"highest_price": 71.85
83
}
84
},
85
}
86
}
87
}
88
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.
JavaScript
1
15
15
1
import json
2
3
# convert json data to a python dict
4
with open('api_data.json') as f:
5
data = json.load(f)
6
7
# data is a python dict
8
# delete unwanted keys and associated data
9
del data['items_list']['AK-47 | Aquamarine Revenge (Minimal Wear)']['marketable']
10
del data['items_list']['AK-47 | Aquamarine Revenge (Minimal Wear)']['tradable']
11
12
# Now, save back as json file
13
with open('api_data.json') as f:
14
json.dump(data, f)
15