ziji zijia
ziji zijia

Reputation: 43

Python API with unorganized json output

I am new to API i followed the documentaion on that site "Flickr" and reached this point where the output is almost a dictionary that i can loop through using keys and values but I am getting and error after trying to json.loads()

import json
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
query = "Hello World".lower().replace(" ", "+")
url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=json&tag={}'.format(query)
res = requests.get(url, headers=headers)
#di = json.loads(res.text)

what I want is to basically convert output into a dictionary that i can loop through.

Here is the output i am getting

>>>res.text
'jsonFlickrFeed({\n\t\t"title": "Uploads from everyone",\n\t\t"link": "https:\\/\\/www.flickr.com\\/photos\\/",\n\t\t"description": "",\n\t\t"modified": "2019-03-16T20:51:09Z",\n\t\t"generator": "https:\\/\\/www.flickr.com",\n\t\t"items": [\n\t   {\n\t\t\t"title": "ffxiv_dx11_2019-03-16_23-51-10",\n\t\t\t"link": "https:\\/\\/www.flickr.com\\/photos\\/146658428@N07\\/32455392917\\/",\n\t\t\t"media"

...

Upvotes: 0

Views: 52

Answers (2)

Alex Hall
Alex Hall

Reputation: 36043

If you just want the raw JSON, with no function wrapper, add the parameter nojsoncallback with a value of 1 to your request.

https://www.flickr.com/services/api/response.json.html

So use:

url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tag={}'.format(query)

Upvotes: 1

Alex Hall
Alex Hall

Reputation: 36043

In general you can just remove the JavaScript function call manually:

prefix = 'jsonFlickrFeed('
assert text.startswith(prefix) and text.endswith(')')
text = text([len(prefix):-1])

Upvotes: 0

Related Questions