0

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]
└─$ 

2 Answers 2

1

If you use get it will not thrown an error if the key is not found and instead return None.

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.get('tim'))

This will print None for the ones missing tim, or you could check first to see if tim is None and only print it if there is a value.

Sign up to request clarification or add additional context in comments.

Comments

0

I figured out my problem. Still learning here.

What happen was in the json file not all lists contained the key 'tim' therefore during my loop when it came to a list with no value 'tim' it would error. I had to check if 'tim' was there.

with open('3714047.json') as file:
    data = json.load(file)
    test = []
    for i in data['posts']:
        test = i.get('tim')
        if test == None:
            pass
        else:
            #print(i['now'], i['name'])
            print(test)

1 Comment

You can do that test more simply: if 'tim' in i: print(i['tim']). (Or you could use `try ... catch KeyError: pass`` to skip processing if you get a KeyError.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.