2

So I have been playing around with Json and some comparing.

So basically I have a Json where some element are included in some elements and some don't.

The issue I am having is that the script continues the script over and over but never does anything due to exception where it can't find the elements etc Price, estimatedLaunchDate which will be found in this Json

sample = {
"threads": [{
        "seoTitle": "used food",
        "other_crap": "yeet"
    },
    {
        "seoTitle": "trucks",
        "other_crap": "it's a fox!"
        "product": {
            "imageUrl": "https://imagerandom.jpg",
            "price": {
                "fullRetailPrice": 412.95
            },
            "estimatedLaunchDate": "2018-06-02T03:00:00.000",
            "publishType ": "DRAW"
        },
        {
            "seoTitle": "rockets",
            "other_crap": "i'm rocket man"
        },
        {
            "seoTitle": "helicopter",
            "other_crap": "for 007"
            "product": {
                "imageUrl": "https://imagerandom.jpg",
                "price": {
                    "fullRetailPrice": 109.95
                },
                "estimatedLaunchDate": "2018-06-19T00:00:00.000",
                "publishType": "FLOW"
            }
        }
    ]
}

as you can see there is some extra information in some of the json elements than the other and there is the issue I am having, What/how can I make it so it continues or just adds etc "Price not found", "publishType not found" and still continue the rest of the json?

I have made a code that does this so far:

old_list = []

    while True:
        try:
            resp = requests.get("www.helloworld.com")
            new_list = resp.json()['threads']

            for item in new_list:

                newitemword = item['seoTitle']

                if newitemword not in old_list:
                    try:

                        print(newitemword) #item name

                        print(str(item['product']['price']['fullRetailPrice']))
                        print(item['product']['estimatedLaunchDate']) 
                        print(item['product']['publishType']) 


                        old_list.append(newitemword)



                    except Exception as e:
                        print(e)
                        print("ERROR")
                        time.sleep(5)
                        continue


            else:
                randomtime = random.randint(40, 60)
                time.sleep(randomtime)

As you can see there is 4 prints inside the try except method and if one of those 3 (fullRetailPrice, estimatedLaunchDate, publishType) it will throw a exception and will not continue the rest of the code meaning it will already die after the "seoTitle": "trucks", element!

2
  • 1
    when you use ['foo'] to get the element at 'foo' from a dictionary, it will raise a KeyError if that element isn't found. If instead you use d.get('foo') then that method will return None (or another default value you provide) if the key 'foo' isn't found. Using that, you can remove the try-except block by checking if the return value from get is None or not. I can write up an example if needed. Commented Jun 27, 2018 at 23:14
  • @BowlingHawk95 I would like to have a example if you have a spare time! I would really appreciate it ! :) Commented Jun 27, 2018 at 23:16

1 Answer 1

4

I won't rewrite your whole example, but suppose that when you lookup the value

print(str(item['product']['price']['fullRetailPrice']))

one or more of those keys you follow might not be there. You could detect this in code by doing

foo = item.get('product', {}).get('price', {}).get('fullRetailPrice')
if foo:
    print(str(foo))
else:
    print('Retail price could not be found')

The second parameter to get is the value to return if the desired key can't be found. Making this return an empty dictionary {} will let you keep checking down the line until the end. The last get will return None if any of the keys aren't found. You can then check if foo is None and print an error message appropriately.

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

3 Comments

Thank you very much! I have a question if I might ask? Etc - lets say if I don't find value of fullRetailPrice and publishType, would I need to do another if else statement for each element?
That sort of depends on the structure of the data you're walking and the logic you want to have. You could write your code to continue to the next item in the list at the first failure, or keep going with the current item. It's really up to you, but you'll probably want one of these if-else blocks per path you try to walk, since I'm guessing that you want to check each path down the object independently of each other.
Oh yeah, I mean it does work with the if else statement and as you said, I do want to keep going on with the current item of course. but I thought maybe there is more "flexible" way etc using nested dictionary values but I don't know if that would fit my "thread" that im trying to do now?

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.