Home > Back-end >  Trying to get specific values from json
Trying to get specific values from json

Time:09-06

Can I get specific values from the json?

I'm trying to get the id, but I don't know how exactly to do it.

This is the json:

{
    "searchType": "Movie",
    "expression": "Lord Of The Rings 2022.json",
    "results": [{
        "id": "tt18368278",
        "resultType": "Title",
        "image": "https://m.media-amazon.com/images/M/MV5BZTMwZjUzNGMtMWI3My00ZGJmLWFmYWEtYjk2YWYxYzI2NWRjXkEyXkFqcGdeQXVyODY0NzcxNw@@._V1_Ratio1.7600_AL_.jpg",
        "title": "The Lord of the Rings Superfans Review the Rings of Power",
        "description": "(2022 Video)"
    }],
    "errorMessage": ""
}

I just want to get the values of result, but I want get specific values, for example the id.

This is my code:

import requests
import json

movie = input("Movies:")
base_url = ("https://imdb-api.com/en/API/SearchMovie/myapi/" movie)
r = (requests.get(base_url))
b = r.json()
print(b['results'])

CodePudding user response:

Considering your json valid, and to accommodate more than one result, you could do:

[...]
r = (requests.get(base_url))
b = r.json()
for result in b['results']:
    print(result['id'])

To get just one item (first item from array), you can do:

 print(b['results'][0]['id'])

Requests documentation: https://requests.readthedocs.io/en/latest/

  • Related