Python/Django Referencing Nested JSON - python

I'm using the Google Places Reverse Geocode API to return the latitude and longatude of a given address. The API returns a massive JSON file with tons of nested objects. Here's Google's documentation on it.
I'm trying to grab the geometry.location.lat object (see documentation). Here's my current code, followed by the error it returns:
address_coords = gmaps.geocode(raw_address)
# gmaps makes the API request and returns the JSON into address_coords
address_lat = address_coords['results']['geometry']['location']['lat']
address_lon = address_coords['results']['geometry']['location']['lng']
TypeError: List indices must be integers or slices, not str
I'm not sure how to reference that JSON object.

The error is telling you that you're trying to index an array as if it were a property.
Most likely results is the array, so you'd need to do the following to get the value for the first item at index 0:
address_coords['results'][0]['geometry']['location']['lng']
The other possibility is that geometry contains multiple coordinates which you could similarly index:
address_coords['results']['geometry'][0]['location']['lng']
Either way, you should pretty print the structure to see which attributes are arrays versus dictionaries.
import pprint
pprint.pprint(address_coords)

Related

How to merge two json arrays in Python

I am looping through a WebService in Python which returns me partially data. I am calling the WebService until i receive an "End of Data" in the response.
The returned objects are always the same structure. They are only part of a large data which the WebService returns in chunks of 1000.
I am saving the returned JSON string in a data variable. How can i copy JSON array data_next to JSON array in variable `data' or merge both JSON files.
Let say you receive something like resp1 = '{"data": [...]}' and store it in a dictionary d. Assuming you do that using json package as follows:
d = json.loads(resp1)
For the next batch of data, you should store it in a temporal dictionary td, extract the field "data_next" and append it into the original dictionary:
td = json.loads(respN)
d["data"].append(td["data_next"])
Let us consider two list A =[{"a":"apple"},{"b":"banana"}]
and list B = [{"c":"cat"}]
try below in python 3 to get merged array.
A.__add__(B)

How to find length of list in byte format?

I have a data variable which contains a list of dicts of lists that i have fetched from an api, formatted as bytes. The list is variable in length.
In: data
Out: b'[{"dict1_list1":[1,2,3],"dict1_list2":[4,5,6]},{"dict2_list1":[1,2,3],"dict2_list2":[4,5,6]}]'
I want to identify when there is only one dict in the list (i.e. the api has run out of data)
In: data
Out: b'[{"onlydict_list1":[1,2,3],"onlydict_list2":[4,5,6]}]'
but because data is formatted as byte I cannot return the length of the list.
In: len(data)
Out: 53 # Not '1' as I would like
How can I identify the length of the list within the b'[....]'?
If r is response then try calling r.json() method . It will convert to a list and then you can find the length of it . You can also use json.loads(r.text) by importing json(import json).
You're getting the length of the bytes object, which is apparently the type of data, not a Python version of the JSON object to which it corresponds.
A simple way to do it would be to first convert the data into the Python object equivalent of the byte-string using json.loads(). Here's what I mean:
import json
data = (b'[{"dict1_list1":[1,2,3],"dict1_list2":[4,5,6]},'
b'{"dict2_list1":[1,2,3],"dict2_list2":[4,5,6]}]')
print(len(json.loads(data))) # -> 2

Converting/Serializing dictionaries containing a large list of value pairs into a json file in python

Currently I try to convert data from an ESRI shapefile (.shp) to a Json file using the json package.
In this process, I want to convert a dictionairy containing the coordinates of a lot of different points:
json.dumps({"Points" : coordinates})
The list "coordinates" looks like:
[[-2244.677490234375, -3717.6876220703125], [-2252.7623006509266, -3717.321774721159],
..., [-2244.677490234375, -3717.6876220703125]]
and contains about several hundreds of coordinate pairs.
However, when I try to execute json.dumps, I get the following error:
[-2244.677490234375, -3717.6876220703125] is not JSON serializable
My first thought was, that it can not handle decimal/float values But if I execute the following working example containing only two of the coordinate pairs:
print(json.dumps({"Points" : [[-2244.677490234375, -3717.6876220703125],
[-2244.677490234375, -3717.6876220703125]]}))
tt works and I do not get an error... The output in this case is:
{"Points": [[-2244.677490234375, -3717.6876220703125], [-2244.677490234375, -3717.6876220703125]]}
I don't get why it isn't working with my "coordinates"-list.
The error you are seeing most commonly happens with custom classes. So I believe your problem has to do with the way pyshp is supplying the coordinate values. I can't be sure without seeing your code, but looking at the pyshp source I found an _Array class that is used in a few places.
class _Array(array.array):
"""Converts python tuples to lits of the appropritate type.
Used to unpack different shapefile header parts."""
def __repr__(self):
return str(self.tolist())
The __repr__ might explain why you believe you are seeing a standard list or tuple when in fact it is a custom class. I put together a python fiddle which demonstrates the exception when providing pyshp's _Array class to json.dumps().
To fix this issue you should pass coordinates.tolist() to your dumps call.
json.dumps({"Points" : coordinates.tolist()})

Serializing and deserializing group elements in Charm-Crypto

I need help to convert the list representation (string) of elliptic curve pairing group element back to an object.
Explanation:
I am using charm crypto v0.43 to use CPABE scheme in my web application. I need to store the pairing element that is generated into my database. When I did that, it is storing it as list. I need to map it back to pairing group element object.
Convert:
[771281202364725359015275543519860265278248937600027018972741977722880288402810467924555583431036045929994018001453439703799448692568829067937466068366897, 5928426678871551838449004494119368314093842432141364739971117261348155912543962117516776973405646414105108743246860323245910977796691638669773215755592297]
to
<pairing.Element object at 0x7f1a1e840300>`
Code:
group = PairingGroup('SS512')
alpha = group.random(ZR)
g= group.random(G1)
Found a solution to my problem. Here is what i did.You can use groups serialize function.
g = group.random(G1) # generates a random group element in that pairing group
You can serialize g using groups serialize function:
str = group.serialize(g)
You can deserialize it using
obj = group.deserialize(str)
Hope it might help somebody facing the same issue.

Accessing Python object in tuple

I am using easyzone and dnspython to extract DNS records from a zone file. When extracting A records I am given back a string and an object in a tuple. I am new to Python coming from PHP and am not quite sure how to get at this object to get the value of it? I had no problems getting the string value in the tuple.
In this code snippet I iterate through the A records and write the values into a CSV:
# Write all A records
for a in z.names.items():
c.writerow([domain, 'A', a.__getitem__(0), a])
a contains the following:
('www.121dentalcare.com.', <easyzone.easyzone.Name object at 0x1012dd190>)
How would I access this object within a which is in the 2nd half of this tuple??
You can use indices to get items from a tuple:
sometuple[1]
just as you can do with lists and strings (see sequence types).
The documentation of easyzone is a little on the thin side, but from looking at the source code it appears the easyzone.easyzone.Name objects have .name, .soa and .ttl attributes:
print sometuple[1].name
The .soa attribute is another custom class, with .mname, .rname, .serial, .refresh, .retry, .expire and .minttl properties.

Categories