Reputation: 551
How to extract 41677?
My json:
{"41677":{"key":"ilya","premium":"true"}}
My code:
params={"id": "ilya", "fmt": "json"}
r=requests.get("somesite", params=params )
data=json.loads(r.text)
Upvotes: 0
Views: 1057
Reputation: 4059
import json
s = {"41677":{"key":"ilya","premium":"true"}}
d = json.dumps(s)
l = json.loads(d)
l.keys()
Upvotes: 0
Reputation: 1
By using Requests' built-in json attribute:
data = requests.get("somesite", params=params ).json().keys()[0]
assuming the json it returns is {"41677":{"key":"ilya","premium":"true"}}:
>>>print data
"41677"
Upvotes: 0
Reputation: 1610
By using loads
, your JSON string will be converted to a dictionary which maps keys to values.
Since you need the key 41677
, you can simply call data.keys()[0]
to retrieve the first key of your dictionary.
EDIT:
Also, if you have a list of that JSON structure, you can iterate over the keys and values using the items
function, like so:
for key, value in data.items():
print key # 41677
print value # {"key":"ilya","premium":"true"}
Upvotes: 3