0

I was wondering how I could go about splitting this json into 3 different movie types in order for me to better retrieve each movies rating and then return a list, showing them from highest to lowest rating. I have tried pythons split("}") but then I got an error saying it's no longer a json object and should not be a list when I am trying to perform my other operations such convertToJSON() function. This also didn't work well to split the movies correctly

import json

def convertToJSON(movieData):
    return json.loads(movieData)

#JSON string python object

Data = """[{ "title": “Spider-Man Homecoming”, 

“Studio: “Marvel”, “Rating”: 98, “videoType: “Movie" },

{ "title": “Avengers Endgame”, 
“Studio: “Marvel”, “Rating”: 90, “videoType: “Movie" },

{ "title": “Godzilla vs  Kong”, 
“Studio: “
    Warner Bros. Pictures”, “Rating”: 89, “videoType: “Movie" }]”””

I am trying to split to return somehow a list like this:

'Spider-Man Homecoming': 98
'Avengers Endgame': 90
'Godilla vs Kong': 89

4 Answers 4

4

Data is a json string consisting of a list of dictionaries. Simply breaking it up into substrings results in invalid json and produces the error you described. The simplest solution would be to decode the entire json string up front and then get the information you want from the result list of dicts:

>>> Data = """
[{ "title": "Spider-Man Homecoming", 
"Studio": "Marvel", "Rating": 98, "videoType": "Movie" },

{ "title": "Avengers Endgame", 
"Studio": "Marvel", "Rating": 90, "videoType": "Movie" },

{ "title": "Godzilla vs  Kong", 
"Studio": "Warner Bros. Pictures", "Rating": 89, "videoType": "Movie" }]
"""
>>> import json
>>> movies = json.loads(Data)
>>> movies
[{'title': 'Spider-Man Homecoming',
  'Studio': 'Marvel',
  'Rating': 98,
  'videoType': 'Movie'},
 {'title': 'Avengers Endgame',
  'Studio': 'Marvel',
  'Rating': 90,
  'videoType': 'Movie'},
 {'title': 'Godzilla vs  Kong',
  'Studio': 'Warner Bros. Pictures',
  'Rating': 89,
  'videoType': 'Movie'}]

>>> {m['title']: m['Rating'] for m in movies}
{'Spider-Man Homecoming': 98, 'Avengers Endgame': 90, 'Godzilla vs  Kong': 89}

(Note the Data string in your post contains typos and non-ASCII quotation marks.)

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

Comments

3

Not really sure whats wrong with your JSON data, but I tried to prettify it a bit, this might not be what you're looking for, but it gets the job done :)

import json

Data = '''[{ "title": "Spider-Man Homecoming",

"Studio": "Marvel", "Rating": 98, "videoType": "Movie" },

{ "title": "Avengers Endgame",
"Studio": "Marvel", "Rating": 90, "videoType": "Movie" },

{ "title": "Godzilla vs  Kong",
"Studio": "Warner Bros. Pictures", "Rating": 89, "videoType": "Movie" }]'''


movies = json.loads(Data)

ratings = {m['title']: m['Rating'] for m in movies}

print(ratings)

1 Comment

Thanks for getting back! This is very much what I was looking for thanks! If I wanted to retrieve the actual rating value and add a value to it, how would I go about doing this? For example I retrieved 89 rating for Godzilla and I wanted to add 4 so the rating is now 93. How would I do this?
1

At most you could make a dict out of it.
with json.loads()
And you can then print the dictionary the way you wanted it down there.
A list would not work because it can only be [value, value, value]

Comments

1

As the previous poster suggests, it would be good loading it with json.loads() and then iterating over it with something like

for movie in json.loads(Data):
   [...]

you can then access it with movie['title'] and build your list

PS: your posted json data has some problems when i tried it on https://jsonlint.com

bit prettier here:

Data = """[{
        "title": "Spider - Man Homecoming",
        "Studio": "Marvel",
        "Rating": 98,
        "videoType": "Movie"
    },
    {
        "title": "Avengers Endgame",
        "Studio": "Marvel",
        "Rating": 90,
        "videoType": "Movie"
    },

    {
        "title": "Godzilla vs Kong",
        "Studio": "Warner Bros.Pictures",
        "Rating": 89,
        "videoType": "Movie"
    }
]"""

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.