how I should proceed to extract the value id for keys from the .json file in python using the keys

105 Views Asked by At

I am a beginner in python. Could you kindly advise me on how I should proceed to extract the value id for keys from the .json file in python. I am currently comparing the key using 'if' condition and then i am stuck as to how I should proceed to get the relevant value.I have written the json contents into a file my code is : ( this is for a YOUTUBE API)

f = open('python_JSON_YOUTUBE.json', 'w+')
        s=str(data)
        f.write(s)
        c = f.read()
        if c == "\"items\"":
            if c == "\"kind\"":
                cursor1.execute ("""INSERT INTO youtube_channel (kind) VALUES(%s)""", ( , ))
1

There are 1 best solutions below

6
On BEST ANSWER

Import the JSON module:

>>> import json

Now, having a JSON formatted string:

>>> s = '{"firstname": "John","lastname": "Smith"}'

Load the string (variable dictionary becomes a dictionary):

>>> dictionary = json.loads(s)

Just operate with dictionary as you would with any Python dictionary, i.e.:

>>> dictionary['firstname']
John