Understanding JSON

W

WallyDD

Hello,

I am trying to geocode some map data using the google maps API.

By using the urllib I can get the JSON output;
http://maps.google.com/maps/geo?q=New+York+USA&output=json&oe=utf8&sensor=true&key=your_api_key

I then read it using;
gmapiresult = json.loads(fg.read())
somedata = gmapiresult['Placemark']

result is; (somedata)
[{u'Point': {u'coordinates': [-73.986951000000005, 40.756053999999999,
0]}, u'ExtendedData': {u'LatLonBox': {u'west': -74.25909, u'east':
-73.699793, u'north': 40.917498999999999, u'south':
40.477383000000003}}, u'AddressDetails': {u'Country': {u'CountryName':
u'USA', u'AdministrativeArea': {u'AdministrativeAreaName': u'NY',
u'Locality': {u'LocalityName': u'New York'}}, u'CountryNameCode':
u'US'}, u'Accuracy': 4}, u'id': u'p1', u'address': u'New York, NY,
USA'}]


Can I further refine the data in "somedata" or am I reliant on string
manipulation?
all I need to get is the data in "coordinates".

I am not sure if this is a json problem or if I don't understand lists
well enough.

Thanks for any help.
 
C

Chris Rebert

Hello,

I am trying to geocode some map data using the google maps API.

By using the urllib I can get the JSON output;
http://maps.google.com/maps/geo?q=New+York+USA&output=json&oe=utf8&sensor=true&key=your_api_key

I then read it using;
gmapiresult = json.loads(fg.read())
somedata = gmapiresult['Placemark']

result is; (somedata)
[{u'Point': {u'coordinates': [-73.986951000000005, 40.756053999999999,
0]}, u'ExtendedData': {u'LatLonBox': {u'west': -74.25909, u'east':
-73.699793, u'north': 40.917498999999999, u'south':
40.477383000000003}}, u'AddressDetails': {u'Country': {u'CountryName':
u'USA', u'AdministrativeArea': {u'AdministrativeAreaName': u'NY',
u'Locality': {u'LocalityName': u'New York'}}, u'CountryNameCode':
u'US'}, u'Accuracy': 4}, u'id': u'p1', u'address': u'New York, NY,
USA'}]


Can I further refine the data in "somedata" or am I reliant on string
manipulation?
all I need to get is the data in "coordinates".

I am not sure if this is a json problem or if I don't understand lists
well enough.

What you get back from the JSON is just some nested lists and
dictionaries containing the data, not a giant string (wouldn't be a
very useful serialization format then, now would it?).
If you pretty-print the data using pprint.pprint(), it becomes much
more readable (I hope linewrap did not get activated...):

[{u'AddressDetails': {u'Accuracy': 4,
u'Country': {u'AdministrativeArea':
{u'AdministrativeAreaName': u'NY',

u'Locality': {u'LocalityName': u'New York'}},
u'CountryName': u'USA',
u'CountryNameCode': u'US'}},
u'ExtendedData': {u'LatLonBox': {u'east': -73.699793,
u'north': 40.917498999999999,
u'south': 40.477383000000003,
u'west': -74.25909}},
u'Point': {u'coordinates': [-73.986951000000005, 40.756053999999999, 0]},
u'address': u'New York, NY,USA',
u'id': u'p1'}]

So to get the coordinates, you simply do:
coords = somedata[0]['Point']['coordinates']

which gets the first and only element in the toplevel list, and this
element is a dictionary,
and then gets the value associated with the key 'Point' in that
dictionary, which gives us another dictionary (that happens to have
only one key-value pair),
and finally gets the value associated with the key 'coordinates' in
that dictionary, which gives us the desired list of floats.

Cheers,
Chris
 
W

WallyDD

I am trying to geocode some map data using the google maps API.
I then read it using;
gmapiresult = json.loads(fg.read())
somedata = gmapiresult['Placemark']
result is; (somedata)
[{u'Point': {u'coordinates': [-73.986951000000005, 40.756053999999999,
0]}, u'ExtendedData': {u'LatLonBox': {u'west': -74.25909, u'east':
-73.699793, u'north': 40.917498999999999, u'south':
40.477383000000003}}, u'AddressDetails': {u'Country': {u'CountryName':
u'USA', u'AdministrativeArea': {u'AdministrativeAreaName': u'NY',
u'Locality': {u'LocalityName': u'New York'}}, u'CountryNameCode':
u'US'}, u'Accuracy': 4}, u'id': u'p1', u'address': u'New York, NY,
USA'}]
Can I further refine the data in "somedata" or am I reliant on string
manipulation?
all I need to get is the data in "coordinates".
I am not sure if this is a json problem or if I don't understand lists
well enough.

What you get back from the JSON is just some nested lists and
dictionaries containing the data, not a giant string (wouldn't be a
very useful serialization format then, now would it?).
If you pretty-print the data using pprint.pprint(), it becomes much
more readable (I hope linewrap did not get activated...):

[{u'AddressDetails': {u'Accuracy': 4,
                      u'Country': {u'AdministrativeArea':
{u'AdministrativeAreaName': u'NY',

u'Locality': {u'LocalityName': u'New York'}},
                                   u'CountryName': u'USA',
                                   u'CountryNameCode': u'US'}},
  u'ExtendedData': {u'LatLonBox': {u'east': -73.699793,
                                   u'north': 40.917498999999999,
                                   u'south': 40.477383000000003,
                                   u'west': -74.25909}},
  u'Point': {u'coordinates': [-73.986951000000005, 40.756053999999999, 0]},
  u'address': u'New York, NY,USA',
  u'id': u'p1'}]

So to get the coordinates, you simply do:
coords = somedata[0]['Point']['coordinates']

which gets the first and only element in the toplevel list, and this
element is a dictionary,
and then gets the value associated with the key 'Point' in that
dictionary, which gives us another dictionary (that happens to have
only one key-value pair),
and finally gets the value associated with the key 'coordinates' in
that dictionary, which gives us the desired list of floats.

Cheers,
Chris

Excellent.
Thank you very much.

pprint looks very helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top