0
import requests
r = requests.get('test.com')
x=r.json()
print(x)

output is

{"test1":858,"test1":154343,"test":106091}

but x's type is str, how to get key,value from x ? i want to get only 858

  • Does this answer your question? [Convert JSON string to dict using Python](https://stackoverflow.com/questions/4528099/convert-json-string-to-dict-using-python) – Omar Al-Howeiti Jul 22 '21 at 18:55

3 Answers3

0

The json function turns the response to a Python dict essentially, so you can use the keys() function or enum unpacking to get the keys and from there obtain the values.

Guy Marino
  • 429
  • 3
  • 6
0

Hope that help you:D

for k in x:
    print(x[k])
Vincent55
  • 33
  • 3
0

You can use the built-in json module:

import requests
import json

r = requests.get('test.com')
x = r.json()

print(json.loads(x)['test1'])

Output:

858
Red
  • 26,798
  • 7
  • 36
  • 58