Python json object get value

While I am trying to retrieve values from JSON string, it gives me an error:

data = json.loads['{"lat":444, "lon":555}']
return data["lat"]

But, if I iterate over the data, it gives me the elements [lat and lon], but not the values:

data = json.loads['{"lat":444, "lon":555}']
    ret = ''
    for j in data:
        ret = ret + ' ' + j
return ret

Which returns: lat lon

What do I need to do to get the values of lat and lon? [444 and 555]

Francesco Boi

7,47412 gold badges69 silver badges109 bronze badges

asked Sep 10, 2012 at 14:00

BrunoVillanovaBrunoVillanova

1,1211 gold badge7 silver badges14 bronze badges

4

If you want to iterate over both keys and values of the dictionary, do this:

for key, value in data.items[]:
    print[key, value]

Neuron

4,5754 gold badges32 silver badges53 bronze badges

answered Sep 10, 2012 at 14:04

0

What error is it giving you?

If you do exactly this:

data = json.loads['{"lat":444, "lon":555}']

Then:

data['lat']

SHOULD NOT give you any error at all.

answered Sep 10, 2012 at 14:04

Pablo Santa CruzPablo Santa Cruz

172k31 gold badges237 silver badges290 bronze badges

Using Python to extract a value from the provided Json

Working sample:

import json
import sys

# load the data into an element
data = {"test1": "1", "test2": "2", "test3": "3"}

# dumps the json object into an element
json_str = json.dumps[data]

# load the json to a string
resp = json.loads[json_str]

# print the resp
print[resp]

# extract an element in the response
print[resp['test1']]

Neuron

4,5754 gold badges32 silver badges53 bronze badges

answered Feb 19, 2016 at 16:23

1

There's a Py library that has a module that facilitates access to Json-like dictionary key-values as attributes: //github.com/asuiu/pyxtension You can use it as:

j = Json['{"lat":444, "lon":555}']
j.lat + ' ' + j.lon

answered Oct 17, 2015 at 19:22

asuasu

4895 silver badges15 bronze badges

Using your code, this is how I would do it. I know an answer was chosen, just giving additional options.

data = json.loads['{"lat":444, "lon":555}']
    ret = ''
    for j in data:
        ret = ret+" "+data[j]
return ret

When you use "for" in this manner you get the key of the object, not the value, so you can get the value by using the key as an index.

Jon

3102 silver badges10 bronze badges

answered Mar 1, 2013 at 23:43

DestreyfDestreyf

4315 silver badges7 bronze badges

1

How do you print the value of a JSON object in Python?

First of all, we are using json. loads[] to create the json object from the json string..
The json. dumps[] method takes the json object and returns a JSON formatted string. The indent parameter is used to define the indent level for the formatted string..

How do you access a JSON object in Python?

It's pretty easy to load a JSON object in Python. Python has a built-in package called json, which can be used to work with JSON data. It's done by using the JSON module, which provides us with a lot of methods which among loads[] and load[] methods are gonna help us to read the JSON file.

How extract specific data from JSON Python?

So first thing you need to import the 'json' module into the file. Then create a simple json object string in python and assign it to a variable. Now we will use the loads[] function from 'json' module to load the json data from the variable. We store the json data as a string in python with quotes notation.

How can I get specific data from JSON?

Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot [ . ] after response [the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function] is how you access the values you want from the JSON object.

Chủ Đề