Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding JSON key to field generated by usaddress python library

Currently I’m using the usaadress python library to parse US addresses https://github.com/datamade/usaddress

Example code

address = '456 Elm St, Someville, NY 54321'
print( usaddress.tag(address))

and the generated results is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> ({'AddressNumber': '456', 'StreetName': 'Elm', 'StreetNamePostType':
> 'St', 'PlaceName': 'Someville', 'StateName': 'NY', 'ZipCode':
> '54321'}, 'Street Address')

All the JSON values have keys besides "Street Address", is there anyway to assign a key to the "Street Address" value also? It will make parsing the JSON using GSON or Jackson much more simple

>Solution :

It looks like usaddress.tag(address) returns a two-element tuple, where the first part is a dictionary that contains all the address components, and the second part is a simple string that describes what kind of address it is — in this case it is a street address, but other possibilities are a PO box, an intersection, etc.

Capture the output from the function:

address_parts, address_type = usaddress.tag(address)

Then create a new key in the dictionary to save the address type:

address_parts['AddressType'] = address_type
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading