-2

I’m using an API to get info about an IMDb page for a given movie (in this case, Avengers: Endgame). The API works, but it returns something that looks like a dictionary, but doesn’t format when ran through usual pprint functions.

('{"Search":[{"Title":"Avengers: '
 'Endgame","Year":"2019","imdbID":"tt415479
6","Type":"movie","Poster":"https://m.media
-amazon.com/images/M/MV5BMTc5MDE2ODcwNV5BMl
5BanBnXkFtZTgwMzI2NzQ2NzM@._V1_SX300.jpg"},
{"Title":"Avengers: '
 'Endgame","Year":"2019","imdbID":"tt415479
6","Type":"movie","Poster":"https://m.media
-amazon.com/images/M/MV5BMTc5MDE2ODcwNV5BMl
5BanBnXkFtZTgwMzI2NzQ2NzM@._V1_SX300.jpg"},
{"Title":"Avengers: '
 'Endgame and the Latest Captain Marvel '
 'Outrage!!","Year":"2019","imdbID":"tt1002
5738","Type":"movie","Poster":"https://m.me
dia-amazon.com/images/M/MV5BZjg2ZTM3OTgtY2E
xMS00OGM4LTg3NDEtNjQ0MjJiZDFmMGFkXkEyXkFqcG
deQXVyMDY3OTcyOQ@@._V1_SX300.jpg"},{"Title"
:"Marvel '
 "Studios' Avengers: Endgame LIVE Red Carpe
t World "
 'Premiere","Year":"2019","imdbID":"tt10240
638","Type":"movie","Poster":"https://m.med
ia-amazon.com/images/M/MV5BNThjZDgwZTYtMjdm
Yy00ZmUyLTk4NTUtMzdjZmExODQ3ZmY4XkEyXkFqcGd
eQXVyMjkzMDgyNTg@._V1_SX300.jpg"},{"Title":
"Avengers '
 'Endgame: the Butt '
 'Plan","Year":"2019","imdbID":"tt10399328"
,"Type":"movie","Poster":"https://m.media-a
mazon.com/images/M/MV5BNTQ1OWQzODktMTY3Zi00
OTQxLWExOTYtZTNjZjY5ZTY4M2UyXkEyXkFqcGdeQXV
yMTAzMzk0NjAy._V1_SX300.jpg"}],"totalResult
s":"5","Response":"True"}')

How do I make this more readable?

  • 5
    It's a JSON string. Parse it to a dictionary and then use `pprint` on it. – esqew Dec 09 '21 at 17:18
  • 1
    @AsherRosenberg, _How_ are you retrieving this? If you're using the `requests` module to access your API, f/e, it has methods that will have it do JSON decoding for you. – Charles Duffy Dec 09 '21 at 18:29

1 Answers1

-2

You can use the json functionality for that:

import json

print(json.dumps(your_json, indent=4))

Edit: this indeed assumes that you have parsed it to a native Python structure before.

Mariusmarten
  • 255
  • 3
  • 15