-1

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)""", ( , ))
  • Please give an example of such a JSON file. – Martin Thoma Jun 12 '15 at 10:30
  • First note: Please use [`with`](http://stackoverflow.com/q/1369526/562769) for handling files. – Martin Thoma Jun 12 '15 at 10:31
  • 1
    I also don't understand where exactly your problem is. – Martin Thoma Jun 12 '15 at 10:31
  • Please, rewrite your question, I don't think it is clear this way. Considering a JSON is formed with attribute-value pairs: which attribute/value do you want to find the JSON? Consider posting the JSON file contents if it is not too big (or the smallest possible version you can make). – Peque Jun 12 '15 at 10:56

1 Answers1

0

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
Peque
  • 13,638
  • 11
  • 69
  • 105
  • Thanks, I am trying to read a JSON page which I have recieved as a response to a google api call.As such the attributes in the page have some repetitive occurrences. So instead of hard-coding this into a dictionary 's' which I presume is what you have suggested( I am just a beginner). How do I extract the values for the requied attributes only? – Issac David Jun 16 '15 at 08:27
  • "kind": "youtube#channelListResponse", "etag": "\"eYE31WLho912TfxEBDDRSwEQ5Ms/4lyuTHXjoFEXQGVGu13p1k74jow\"" – Issac David Jun 16 '15 at 08:29
  • as such I would like to receive all the possible values for "kind" and "etag" for multiple api calls – Issac David Jun 16 '15 at 08:29
  • @IssacDavid: please, open a Python console (or iPython if you prefer) and experiment with the code I provided. That code is enough for your purpose, you just need to understand it. – Peque Jun 16 '15 at 08:51
  • Thank you.My actual application is to enter the respective values for over 500 entries into a database in MySQL (in python) for the suitable multiple attributes.I am looking at using the api call to get the json page as response and further enter the attributes from that json page into the MySQL database.I hope I'm not wrong in understanding, that if I create a dictionary it would suffice as long as I have the necessary number of json formatted strings for the dictionary – Issac David Jun 16 '15 at 10:48
  • @IssacDavid: having an array/list of responses, each of them being a JSON formatted string, you just need to iterate over that list in a loop. Then, for each JSON formatted string, load it to a dictionary as showed above in my answer. Then, accessing the values you are looking for is straight forward. Once you have those values, store them in you DB or do whatever you want with them. Keep iterating over the array/list until you are done. – Peque Jun 16 '15 at 10:51