I have a json object coming from an api call to TMDb(Movie Database) and it looks like the following:
{
"page": 1,
"results": [
{
"adult": false,
"backdrop_path": "/8Y43POKjjKDGI9MH89NW0NAzzp8.jpg",
"genre_ids": [
35,
28,
12,
878
],
"id": 550988,
"original_language": "en",
"original_title": "Free Guy",
"popularity": 11660.794,
"poster_path": "/xmbU4JTUm8rsdtn7Y3Fcm30GpeT.jpg",
"release_date": "2021-08-11",
"title": "Free Guy",
"video": false,
"vote_average": 8,
"vote_count": 1719
},
{
"adult": false,
"backdrop_path": "/aO9Nnv9GdwiPdkNO79TISlQ5bbG.jpg",
"genre_ids": [
28,
12
],
"id": 568620,
"original_language": "en",
"original_title": "Snake Eyes: G.I. Joe Origins",
"popularity": 6520.868,
"poster_path": "/uIXF0sQGXOxQhbaEaKOi2VYlIL0.jpg",
"release_date": "2021-07-22",
"title": "Snake Eyes: G.I. Joe Origins",
"video": false,
"vote_average": 7,
"vote_count": 531
},
How can I store this into a key value pair, where maybe the movie id will serve as a key and the rest of the data as value. I am new to this and could only think of doing it this way. right now I am doing this:
response = requests.get("https://api.themoviedb.org/3/movie/popular?api_key=###&language=en-US&page=2")
json_data = json.loads(response.text)
print(json_data)
although this does not allow me to access the json elements as key value pairs, rather it's kind of like a textual/raw format of the entire result.
{'page': 2, 'results': [{'adult': False, 'backdrop_path': '/ugukqzx4gSzBd1yzmbWEHLkpGaS.jpg', 'genre_ids': [16, 10751, 14, 35, 10402, 12], 'id': 597316, 'original_lan
guage': 'en', 'original_title': 'My Little Pony: A New Generation', 'overview': "Equestria's divided. But a bright-eyed hero believes Earth Ponies, Pegasi and Unicorn
s should be pals — and, hoof to heart, she’s determined to prove it.", 'popularity': 1316.669, 'poster_path': '/hzq5XRGgm6NDMOW1idUvbpGqEkv.jpg', 'release_date': '202
1-09-23', 'title': 'My Little Pony: A New Generation', 'video': False, 'vote_average': 7.9, 'vote_count': 61}, {'adult': False, 'backdrop_path': '/8s4h9friP6Ci3adRGah
HARVd76E.jpg', 'genre_ids': [16, 35, 10751, 878], 'id': 379686, 'original_language': 'en', 'original_title': 'Space Jam: A New Legacy', 'overview': "When LeBron and h
is young son Dom are trapped in a digital space by a rogue A.I., LeBron must get them home safe by leading Bugs, Lola Bunny and the whole gang of notoriously undiscip
lined Looney Tunes to victory over the A.I.'s digitized champions on the court. It's Tunes versus Goons in the highest-stakes challenge of his life.", 'popularity': 9
04.381, 'poster_path': '/5bFK5d3mVTAvBCXi5NPWH0tYjKl.jpg', 'release_date': '2021-07-08', 'title': 'Space Jam: A New Legacy', 'video': False, 'vote_average': 7.3, 'vot
e_count': 2309}, {'adult': False, 'backdrop_path': '/620hnMVLu6RSZW6a5rwO8gqpt0t.jpg', 'genre_ids': [16, 35, 10751, 14], 'id': 508943, 'original_language': 'en', 'ori
ginal_title': 'Luca', 'overview': 'Luca and his best friend Alberto experience an unforgettable summer on the Italian Riviera. But all the fun is threatened by a deep
ly-held secret: they are sea monsters from another world just below the water’s surface.', 'popularity': 927.5, 'poster_path': '/jTswp6KyDYKtvC52GbHagrZbGvD.jpg', 're
lease_date': '2021-06-17', 'title': 'Luca', 'video': False, 'vote_average': 8, 'vote_count': 4666}, {'adult': False, 'backdrop_path': '/keIxh0wPr2Ymj0Btjh4gW7JJ89e.jp
g', 'genre_ids': [28, 12, 53, 878], 'id': 497698, 'original_language': 'en', 'original_title': 'Black Widow', 'overview': 'Natasha Romanoff, also known as Black Widow
, confronts the darker parts of her ledger when a dangerous conspiracy with ties to her past arises. Pursued by a force that will stop at nothing to bring her down, N
atasha must deal with her history as a spy and the broken relationships left in her wake long before she became an Avenger.', 'popularity': 942.947, 'poster_path': '/
qAZ0pzat24kLdO3o8ejmbLxyOac.jpg', 'release_date': '2021-07-07', 'title': 'Black Widow', 'video': False, 'vote_average': 7.7, 'vote_count': 5231}, {'adult': False, 'ba
ckdrop_path': '/akwg1s7hV5ljeSYFfkw7hTHjVqk.jpg', 'genre_ids': [16, 35, 12, 10751], 'id': 459151, 'original_language': 'en', 'original_title': 'The Boss Baby: Family
Business', 'overview': 'The Templeton brothers — Tim and his Boss Baby little bro Ted — have become adults and drifted away from each other. But a new boss baby with
a cutting-edge approach and a can-do attitude is about to bring them together again … and inspire a new family business.', 'popularity': 931.838, 'poster_path': '/uWS
tkK8bq9ixY3fc7y209ZleCoF.jpg', 'release_date': '2021-07-01', 'title': 'The Boss Baby: Family Business', 'video': False, 'vote_average': 7.8, 'vote_count': 1451}, {'ad
ult': False, 'backdrop_path': '/qD45xHA35HdJDGOaA1AgDwiWEgO.jpg', 'genre_ids': [10749, 18], 'id': 744275, 'original_language': 'en', 'original_title': 'After We Fell'
, 'overview': "Just as Tessa's life begins to become unglued, nothing is what she thought it would be. Not her friends nor her family. The only person that she should
be able to rely on is Hardin, who is furious when he discovers the massive secret that she's been keeping. Before Tessa makes the biggest decision of her life, every
thing changes because of revelations about her family.", 'popularity': 909.3, 'poster_path': '/3WfvjNWr5k1Zzww81b3GWc8KQhb.jpg', 'release_date': '2021-09-01', 'title'
: 'After We Fell', 'video': False, 'vote_average': 8.1, 'vote_count': 284}, {'adult': False, 'backdrop_path': '/iDLtDgxLiYsarfdQ4msUhUqoNPp.jpg', 'genre_ids': [878],
'id': 725273, 'original_language': 'en', 'original_title': 'Solitary', 'overview': "A man wakes up inside a room to discover he's a prisoner sent into space to form E
arth's first colony, and worse - his cell mate Alana is hell bent on destroying everything.", 'popularity': 1194.395, 'poster_path': '/ApwmbrMlsuOay5rXQA4Kbz7qJAl.jpg
', 'release_date': '2021-09-24', 'title': 'Solitary', 'video': False, 'vote_average': 6.7, 'vote_count': 20}, {'adult': False, 'backdrop_path': '/dsdbViTNjLu4DbgkkYmu
Y4xDQ20.jpg', 'genre_ids': [27, 53, 9648], 'id': 585216, 'original_language': 'en', 'original_title': 'Escape Room: Tournament of Champions', 'overview': "Six people
unwittingly find themselves locked in another series of escape rooms, slowly uncovering what they have in common to survive as they discover all the games that they'v
e played before.", 'popularity': 732.827, 'poster_path': '/jGYJyPzVgrVV2bgClI9uvEZgVLE.jpg', 'release_date': '2021-07-01', 'title': 'Escape Room: Tournament of Champi
ons', 'video': False, 'vote_average': 7.1, 'vote_count': 479}, {'adult': False, 'backdrop_path': '/xPpXYnCWfjkt3zzE0dpCNME1pXF.jpg', 'genre_ids': [16, 28, 12, 14], 'i
d': 635302, 'original_language': 'ja', 'original_title': '劇場版「鬼滅の刃」無限列車編', 'overview': "Tanjirō Kamado, joined with Inosuke Hashibira, a boy raised by b
oars who wears a boar's head, and Zenitsu Agatsuma, a scared boy who reveals his true power when he sleeps, boards the Infinity Train on a new mission with the Fire H
ashira, Kyōjurō Rengoku, to defeat a demon who has been tormenting the people and killing the demon slayers who oppose it!", 'popularity': 816.827, 'poster_path': '/h
8Rb9gBr48ODIwYUttZNYeMWeUU.jpg', 'release_date': '2020-10-16', 'title': 'Demon Slayer -Kimetsu no Yaiba- The Movie: Mugen Train', 'video': False, 'vote_average': 8.4,
'vote_count': 1690}, {'adult': False, 'backdrop_path': '/VuukZLgaCrho2Ar8Scl9HtV3yD.jpg', 'genre_ids': [878, 28], 'id': 335983, 'original_language': 'en', 'original_
title': 'Venom', 'overview': 'Investigative journalist Eddie Brock attempts a comeback following a scandal, but accidentally becomes the host of Venom, a violent, sup
er powerful alien symbiote. Soon, he must rely on his newfound powers to protect the world from a shadowy organization looking for a symbiote of their own.', 'popular
ity': 1073.972, 'poster_path': '/2uNW4WbgBXL25BAbXGLnLqX71Sw.jpg', 'release_date': '2018-09-28', 'title': 'Venom', 'video': False, 'vote_average': 6.8, 'vote_count':
11445}, {'adult': False, 'backdrop_path': '/3pIqd1hgZ2xqzWEyiYp4blqE9Fi.jpg', 'genre_ids': [53, 18, 36], 'id': 522241, 'original_language': 'en', 'original_title': 'T
he Courier', 'overview': 'Cold War spy Greville Wynne and his Russian source try to put an end to the Cuban Missile Crisis.', 'popularity': 763.95, 'poster_path': '/z
FIjKtZrzhmc7HecdFXXjsLR2Ig.jpg', 'release_date': '2021-03-18', 'title': 'The Courier', 'video': False, 'vote_average': 7.2, 'vote_count': 399}, {'adult': False, 'back
drop_path': '/nprqOIEfiMMQx16lgKeLf3rmPrR.jpg', 'genre_ids': [28], 'id': 619297, 'original_language': 'en', 'original_title': 'Sweet Girl', 'overview': "A devastated
husband vows to bring justice to the people responsible for his wife's death while protecting the only family he has left, his daughter.", 'popularity': 707.316, 'pos
ter_path': '/cP7odDzzFBD9ycxj2laTeFWGLjD.jpg', 'release_date': '2021-08-18', 'title': 'Sweet Girl', 'video': False, 'vote_average': 6.9, 'vote_count': 618}, {'adult':
False, 'backdrop_path': '/26LIw3oKGPUEGUpb8AXZ64dZZt8.jpg', 'genre_ids': [16, 35, 9648, 10751, 14], 'id': 843906, 'original_language': 'en', 'original_title': 'Strai
ght Outta Nowhere: Scooby-Doo! Meets Courage the Cowardly Dog', 'overview': 'With Mystery, Inc. on the tail of a strange object in Nowhere, Kansas, the strange hometo
wn of Eustice, Muriel, and Courage, the gang soon find themselves contending with a giant cicada monster and her winged warriors.', 'popularity': 698.739, 'poster_pat
h': '/uIdMpWrQ30SHPINsy7LcPFloyvO.jpg', 'release_date': '2021-09-14', 'title': 'Straight Outta Nowhere: Scooby-Doo! Meets Courage the Cowardly Dog', 'video': False, '
vote_average': 8.3, 'vote_count': 171}, {'adult': False, 'backdrop_path': '/kcvKEvTWqIGMjpVJdbDVRHdIt4C.jpg', 'genre_ids': [12, 28, 53], 'id': 370172, 'original_langu
age': 'en', 'original_title': 'No Time to Die', 'overview': 'Bond has left active service and is enjoying a tranquil life in Jamaica. His peace is short-lived when hi
s old friend Felix Leiter from the CIA turns up asking for help. The mission to rescue a kidnapped scientist turns out to be far more treacherous than expected, leadi
ng Bond onto the trail of a mysterious villain armed with dangerous new technology.', 'popularity': 724.041, 'poster_path': '/iUgygt3fscRoKWCV1d0C7FbM9TP.jpg', 'relea
se_date': '2021-09-29', 'title': 'No Time to Die', 'video': False, 'vote_average': 7.2, 'vote_count': 91}, {'adult': False, 'backdrop_path': '/gzppdxEJ6fofhtLzSVSUJZE
Vxvq.jpg', 'genre_ids': [28, 878, 53], 'id': 848278, 'original_language': 'en', 'original_title': 'Jurassic Hunt', 'overview': 'Female adventurer Parker joins a crew
of male trophy hunters in a remote wilderness park. Their goal: slaughter genetically recreated dinosaurs for sport using rifles, arrows, and grenades. After their gu
ide is killed by raptors, the team tries to escape the park – but the hunters quickly become the hunted. Even worse, the park’s manager suspects Parker of being a spy
and sends a hit squad after her. This battle’s about to become primitive!', 'popularity': 717.184, 'poster_path': '/bZnOioDq1ldaxKfUoj3DenHU7mp.jpg', 'release_date':
'2021-09-01', 'title': 'Jurassic Hunt', 'video': False, 'vote_average': 5, 'vote_count': 138}, {'adult': False, 'backdrop_path': '/piz0BFUJ4Bro4BFzxxkNpsGCmOd.jpg',
'genre_ids': [14, 10751, 27], 'id': 739990, 'original_language': 'en', 'original_title': 'Nightbooks', 'overview': 'Alex, a boy obsessed with scary stories, is trappe
d by a witch in her modern, magical New York City apartment. His original hair-raising tales are the only thing keeping him safe as he desperately tries to find a way
out of this twisted place.', 'popularity': 684.79, 'poster_path': '/ysJte1iqN8pFQ470tumnViB1wHP.jpg', 'release_date': '2021-09-15', 'title': 'Nightbooks', 'video': F
alse, 'vote_average': 6.7, 'vote_count': 125}, {'adult': False, 'backdrop_path': '/oM7AstNYo6eSkgAieXvsIxTPPDv.jpg', 'genre_ids': [53], 'id': 785752, 'original_langua
ge': 'en', 'original_title': 'Intrusion', 'overview': 'When a husband and wife move to a small town, a home invasion leaves the wife traumatized and suspicious that t
hose around her might not be who they seem.', 'popularity': 770.211, 'poster_path': '/o6ozTBflWXlBje0uxJv4m6s4HTq.jpg', 'release_date': '2021-09-22', 'title': 'Intrus
ion', 'video': False, 'vote_average': 6.3, 'vote_count': 168}, {'adult': False, 'backdrop_path': '/xDnFlNrNUoSKPq4uptnhYmUZNpm.jpg', 'genre_ids': [27, 53, 9648, 80],
'id': 619778, 'original_language': 'en', 'original_title': 'Malignant', 'overview': 'Madison is paralyzed by shocking visions of grisly murders, and her torment worse
ns as she discovers that these waking dreams are in fact terrifying realities with a mysterious tie to her past.', 'popularity': 758.072, 'poster_path': '/dGv2BWjzwAz
6LB8a8JeRIZL8hSz.jpg', 'release_date': '2021-09-01', 'title': 'Malignant', 'video': False, 'vote_average': 7.1, 'vote_count': 651}, {'adult': False, 'backdrop_path':
'/AjQgFtfXTmmMVuaH2VfQDdGbeQH.jpg', 'genre_ids': [28, 18, 80], 'id': 706972, 'original_language': 'en', 'original_title': 'Narco Sub', 'overview': 'A man will become
a criminal to save his family. Director: Shawn Welling Writer: Derek H. Potts Stars: Tom Vera, Tom Sizemore, Lee Majors |', 'popularity': 661.522, 'poster_path': '
/7p0O4mKYLIhU2E5Zcq9Z3vOZ4e9.jpg', 'release_date': '2021-01-22', 'title': 'Narco Sub', 'video': False, 'vote_average': 7.1, 'vote_count': 57}, {'adult': False, 'backd
rop_path': '/kDQdFs5XL6G8TuXvd0NnJfQ59GP.jpg', 'genre_ids': [878, 12, 9648, 28, 53, 14, 35], 'id': 641501, 'original_language': 'fr', 'original_title': 'Comment je su
is devenu super-héros', 'overview': "Paris 2020. While superheroes have assimilated into the Parisian society, they discover a new drug that gives themselves personal
superpowers to mere mortals. Lieutenants Moreau and Schaltzmann are investigating the case with the support of two ex-superheroes, Monte Carlo and Callista. They'll
do whatever it takes to dismantle the traffic. But Moreau's past resurfaces, and the investigation becomes more complicated.", 'popularity': 655.765, 'poster_path': '
/d0wn9YtEnmbucx8ZUsT918ctoiK.jpg', 'release_date': '2020-11-19', 'title': 'How I Became a Superhero', 'video': False, 'vote_average': 6, 'vote_count': 260}], 'total_p
ages': 500, 'total_results': 10000}
CodePudding user response:
You can Access to the json elements like this (this code will give you the original title of last object in json data)
print(requests.get(url).json()['results'][-1]["original_title"])
I wish that i helped you
CodePudding user response:
Worth pointing out that there's a TMDB library already, so you don't need requests
https://pypi.org/project/tmdbv3api/
it's kind of like a textual/raw format of the entire result.
No, it's exactly what you asked for - you're printing a Python dictionary...
Requests has a json parser already, though. Use it
From that, parse your data further
r = requests.get(url)
if r.status_code == 200:
data = r.json().get('results', [])
movies = {r['id']: r for r in results}
print(movies)