Reputation: 369
So I have been playing around with Json and Python and basically I have a Json file where sometimes some elements are not included and sometimes it is. Basically what I want to do is that check if the first element that contains "ImageUrl" is there and is valid then use that element, Else use the other imageURL
"threads": [{
"id": "3a64a3b18894fb70c92b6382a1e8f735320c1cbb",
"product": {
"imageUrl": "https://hello.com/555088_401.JPG",
},
"imageUrl": "https://hello.com/images/555088_401.JPG",
}]
However it can happen that the first ImageURL can contain 999999_999
at the end and that is counted as invalid aswell.
What I have done so far is:
resp = s.get(url)
item = resp.json()['threads']
itempic = item.get('imageUrl') # Check if there is sell date
if itempic:
image = str(item['imageUrl']) # Image pic
else:
print('Picture not found')
which currently only take the second url imageUrl which is not what I want but it does work (also it check if the imageUrl contains anything aswell), The question is:
How can I take the first imageUrl element and check if it is "valid" and does not contains 999999_999
at the end (If it is valid and correct then use it and print) else If it is not "valid" then use the second imageUrl
Upvotes: 0
Views: 156
Reputation: 531145
You just need to check whatever value you get back for 999999_999
. It's simpler if you use the get
method to "pretend" that a missing URL is a string that will match.
# This assumes that you want the top-level imageURL in preference to
# the product imageURL...
def get_url(d):
for item in d['threads'], d['threads']['product']:
url = item.get('imageUrl', '999999_999')
if not re.match('999999_999', url):
return url
As you can see, it's also simpler to just pretend that the second try could match 999999_999
, even if you know it won't.
Upvotes: 1