0

I am trying to return a specific field from a json result. Here is what I am trying to do with it:

rjson = json.loads("{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'genre_ids': [35, 18, 10749], u'id': 19913, u'original_language': u'en'}]}")

however, I get the following error when running it:

ValueError: Expecting property name: line 1 column 2 (char 1)

EDIT:

Here is the full program (minus the API key):

import requests, json
r = requests.get('https://api.themoviedb.org/3/find/'+'tt1022603'+'?api_key=###&external_source=imdb_id')
print str(r.json())
rjson = json.loads("{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'genre_ids': [35, 18, 10749], u'id': 19913, u'original_language': u'en'}]}")

where I do the loads() I just put the print from the line above.

5
  • 1
    That string isn't a valid JSON document. Commented Feb 18, 2016 at 16:23
  • You don't have a JSON string. You have Python repr() output for a dictionary. Commented Feb 18, 2016 at 16:23
  • Do you generate that string or do you receive it as program input? If you generate it, can you show us how you did it? Commented Feb 18, 2016 at 16:25
  • Sure, it is through the tmdb api. I can post what I have so far. Commented Feb 18, 2016 at 16:27
  • r.json() already returns a parsed Python dict. There is no need to call json.loads() on it again. Commented Feb 18, 2016 at 16:33

3 Answers 3

1

This is not a valid JSON you are trying to load. It looks more like a string representation of the dictionary. If you cannot change the input data to be a valid JSON, load it via ast.literal_eval() which would eval the string safely:

>>> from ast import literal_eval
>>>
>>> s = "{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'genre_ids': [35, 18, 10749], u'id': 19913, u'original_language': u'en'}]}"
>>> d = literal_eval(s)  # d is a dictionary
>>> d["movie_results"][0]["id"]
19913
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I didn't even realize what I was looking at.
1

That's because your string is not valid JSON, but a python dictionary as a string. What you want is ast.literal_eval.

>>> from ast import literal_eval
>>> literal_eval("{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'genre_ids': [35, 18, 10749], u'id': 19913, u'original_language': u'en'}]}")
{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'id': 19913, u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'original_language': u'en', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'genre_ids': [35, 18, 10749]}]}

2 Comments

What they want is to fix whatever produced that output.
@MartijnPieters alright, substitute "want" with "can do for now", because we have no idea how OP is getting his string.
0

The json() method of the response parses the JSON for you and returns a JSON object. You can access that directly, and there is no need to try and parse it again.

r = requests.get('https://api.themoviedb.org/3/find/'+'tt1022603'+'?api_key=###&external_source=imdb_id')
data = r.json()
print data['tv_season_results']

Comments

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.