I'm trying to parse a json file to extract key values to be used for objectives later. I can print some keys fine, but others don't return every key value in the json. Instead it returns a few then throws an error.
code example:
import json
import wget
#grab = wget.download('https://example.org/p/thread/3714047.json')
with open('3714047.json') as file:
data = json.load(file)
for i in data['posts']:
print(i['now'])
print(i['tim'])
json file example:
{
"posts":[
{
"no":3714047,
"now":"08\/24\/20(Mon)00:45:48",
"name":"",
"sub":"Cameras",
"com":"so this is text.",
"filename":"fujifilm_16589199_x_t3_mirrorless_digital_camera_1536757624_1433842",
"ext":".jpg",
"w":500,
"h":500,
"tn_w":250,
"tn_h":250,
"tim":1598244348346,
"time":1598244348,
"md5":"rqVskH5ul+hexgq5Vl3x8Q==",
"fsize":47138,
"resto":0,
"bumplimit":1,
"imagelimit":0,
"semantic_url":"camera-recommendations",
"replies":326,
"images":21,
"unique_ips":68
},
{
"no":3714058,
"now":"08\/24\/20(Mon)01:32:51",
"name":"",
"com":"some different text.",
"time":1598247171,
"resto":3714047
},
the json file when hand counting has over 20+ 'tim': values. Not all entries has the tim key. here is my print output:
──(c4㉿ib)-[~/Desktop/test]
└─$ python3 a.py 08/24/20(Mon)00:45:48
1598244348346
08/24/20(Mon)00:50:56
1598244656454
08/24/20(Mon)00:56:48
Traceback (most recent call last):
File "a.py", line 11, in <module>
print(i['tim'])
KeyError: 'tim'
┌──(c4㉿ib)-[~/Desktop/test]
└─$
Can anyone see what I am doing wrong? I've tried various ways with this being the closest I've gotten to get the values from the json I want.
When I use only 'now' everything works fine or 'now' with 'name' and so on. guessing it has something to do with tim being a number?
print(i['now'], i['name']):
┌──(c4㉿ib)-[~/Desktop/test]
└─$ python3 a.py 1 ⨯
08/24/20(Mon)00:45:48 Anonymous
08/24/20(Mon)00:50:56 Anonymous
08/24/20(Mon)00:56:48 Anonymous
08/24/20(Mon)01:01:57 Anonymous
08/24/20(Mon)01:22:36 Anonymous
08/24/20(Mon)01:32:51 Anonymous
08/24/20(Mon)01:34:21 Anonymous
08/24/20(Mon)01:35:26 Anonymous
┌──(c4㉿ib)-[~/Desktop/test]
└─$