# Json to ruby, clean up hashes and arrays

**URL:** <https://www.ruby-forum.com/t/json-to-ruby-clean-up-hashes-and-arrays/142886>\
**Category:** Rails\
**Created:** [July 12, 2008, 7:48pm UTC](https://www.ruby-forum.com/t/json-to-ruby-clean-up-hashes-and-arrays/142886 "2008-07-12T19:48:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [July 12, 2008, 7:48pm UTC](https://www.ruby-forum.com/t/json-to-ruby-clean-up-hashes-and-arrays/142886/1 "2008-07-12T19:48:23Z")

</div>

i am trying to convert a json object to a ruby object in a format that  
is easy to work with. i use the json.decode and that converts it to  
ruby as hashes and arrays like it should.  
the part i am trying to figure out is a clean way to reorganize the  
hashes (see my code below).  
the first problem i have, is if any of the fields are missing from the  
json object i get the “You have a nil object when you didn’t expect  
it!” error.  
the second issue is it is ugly and hard to read.

what is a good way to do this?

address = ActiveSupport::JSON.decode(address)  
@address = {}  
@address[‘status’] = address[‘Status’][‘code’]  
@address[‘submitted\_address’] = address[‘name’]  
@address[‘full\_address’] = address[‘Placemark’][0][‘address’]  
@address[‘longitude’] = address[‘Placemark’][0][‘Point’][‘coordinates’]  
[0]  
@address[‘lattitude’] = address[‘Placemark’][0][‘Point’][‘coordinates’]  
[1]  
@address[‘elevation’] = address[‘Placemark’][0][‘Point’][‘coordinates’]  
[2]  
@address[‘accuracy’] = address[‘Placemark’][0][‘AddressDetails’]  
[‘Accuracy’]  
@address[‘country’] = address[‘Placemark’][0][‘AddressDetails’]  
[‘Country’][‘CountryNameCode’]  
@address[‘state’] = address[‘Placemark’][0][‘AddressDetails’]  
[‘Country’][‘AdministrativeArea’][‘AdministrativeAreaName’]  
@address[‘county’] = address[‘Placemark’][0][‘AddressDetails’]  
[‘Country’][‘AdministrativeArea’][‘SubAdministrativeArea’]  
[‘SubAdministrativeAreaName’]  
@address[‘city’] = address[‘Placemark’][0][‘AddressDetails’][‘Country’]  
[‘AdministrativeArea’][‘SubAdministrativeArea’][‘Locality’]  
[‘LocalityName’]  
@address[‘street’] = address[‘Placemark’][0][‘AddressDetails’]  
[‘Country’][‘AdministrativeArea’][‘SubAdministrativeArea’][‘Locality’]  
[‘Thoroughfare’][‘ThoroughfareName’]  
@address[‘zip’] = address[‘Placemark’][0][‘AddressDetails’][‘Country’]  
[‘AdministrativeArea’][‘SubAdministrativeArea’][‘Locality’]  
[‘PostalCode’][‘PostalCodeNumber’]

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [July 12, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/json-to-ruby-clean-up-hashes-and-arrays/142886/2 "2008-07-12T20:42:22Z")

</div>

Hi skud,

skud wrote:

> the first problem i have, is if any of the fields are missing  
> from the json object i get the “You have a nil object when  
> you didn’t expect it!” error.

The easy way to address the first problem, in a brute force way, would  
be  
to add to each line so that, for example, …

> @address[‘status’] = address[‘Status’][‘code’]

becomes…

@address[‘status’] = address[‘Status’][‘code’] unless  
address[‘Status’][‘code’].nil?

> the second issue is it is ugly and hard to read.

Yeah 😉

> what is a good way to do this?
> 
> address = ActiveSupport::JSON.decode(address)  
> @address = {}  
> @address[‘status’] = address[‘Status’][‘code’]  
> @address[‘submitted\_address’] = address[‘name’]  
> @address[‘full\_address’] = address[‘Placemark’][0][‘address’]

One way would, assuming the JSON structure is static, would be to create  
a  
table / model with a couple of columns; an entry for each of your  
@address  
attributes and a corresponding ‘path’ to the JSON location for each of  
your  
@address attributes. The ‘origin’ column would contain strings like  
“address[‘Placemark’][0][‘address’]” and the code would like something  
like…

address = ActiveSupport::JSON.decode(address)  
@address = {}  
address\_mappings = Address\_mappings.find(:all)  
address\_mappings.each do |this\_mapping|  
@address[this\_mapping.destination] = send(this\_mapping.origin)  
end

Definitely NOT elegant. What we call down here in Texas, “lipstick on a  
pig.” 😉 Maybe somebody else will chime in to show us both a better  
way.

HTH,  
Bill
