Jump to content

Using python to access a specific value in JSON

Coaxialgamer
Go to solution Solved by vorticalbox,
3 hours ago, Coaxialgamer said:

I'm attempting to design a small program that returns a specific value from an API. The API returns as a json file with all the values it provides however , there are a multitude of elements in that json , and i only need one ( 'round_duration') .

I've installed the python requests library .

I'm fairly new to python and programming in general, and i don't have much of a clue as how to do this , especially considering this is a nonstandard library ( although a fairly popular one ).

My base code looks like this :


import requests
url = '<sample url>'
a=requests.get(url)
print (a.text)

When i execute this scrypt, i get all the data in the json . However , i only need round_duration . I've looked online but i can't seem to find a clear answer to this

 

Can anyone help with this?

json = JSON.loads(a.text)

print(json['round-duration'])

I'm attempting to design a small program that returns a specific value from an API. The API returns as a json file with all the values it provides however , there are a multitude of elements in that json , and i only need one ( 'round_duration') .

I've installed the python requests library .

I'm fairly new to python and programming in general, and i don't have much of a clue as how to do this , especially considering this is a nonstandard library ( although a fairly popular one ).

My base code looks like this :

import requests
url = '<sample url>'
a=requests.get(url)
print (a.text)

When i execute this scrypt, i get all the data in the json . However , i only need round_duration . I've looked online but i can't seem to find a clear answer to this

 

Can anyone help with this?

AMD Ryzen R7 1700 (3.8ghz) w/ NH-D14, EVGA RTX 2080 XC (stock), 4*4GB DDR4 3000MT/s RAM, Gigabyte AB350-Gaming-3 MB, CX750M PSU, 1.5TB SDD + 7TB HDD, Phanteks enthoo pro case

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Coaxialgamer said:

I'm attempting to design a small program that returns a specific value from an API. The API returns as a json file with all the values it provides however , there are a multitude of elements in that json , and i only need one ( 'round_duration') .

I've installed the python requests library .

I'm fairly new to python and programming in general, and i don't have much of a clue as how to do this , especially considering this is a nonstandard library ( although a fairly popular one ).

My base code looks like this :


import requests
url = '<sample url>'
a=requests.get(url)
print (a.text)

When i execute this scrypt, i get all the data in the json . However , i only need round_duration . I've looked online but i can't seem to find a clear answer to this

 

Can anyone help with this?

json = JSON.loads(a.text)

print(json['round-duration'])

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, geo3 said:

Assuming 'round-duration' is top level.

it is fortunately

AMD Ryzen R7 1700 (3.8ghz) w/ NH-D14, EVGA RTX 2080 XC (stock), 4*4GB DDR4 3000MT/s RAM, Gigabyte AB350-Gaming-3 MB, CX750M PSU, 1.5TB SDD + 7TB HDD, Phanteks enthoo pro case

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, vorticalbox said:

json = JSON.loads(a.text)

print(json['round-duration'])

thanks , that worked . Although there is a slight syntax error .


json = JSON.loads(a.text)

does not work , instead you need this

json = json.loads(a.text)

Also that scrypt requires the json module

AMD Ryzen R7 1700 (3.8ghz) w/ NH-D14, EVGA RTX 2080 XC (stock), 4*4GB DDR4 3000MT/s RAM, Gigabyte AB350-Gaming-3 MB, CX750M PSU, 1.5TB SDD + 7TB HDD, Phanteks enthoo pro case

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, Coaxialgamer said:

thanks , that worked . Although there is a slight syntax error .


json = JSON.loads(a.text)

does not work , instead you need this


json = json.loads(a.text)

Also that scrypt requires the json module

Sorry about that. I use node for work and json is included. Glad you worked it out.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×