Working with Rails involves making API calls that return JSON data:
{"observations":{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","observation_start":"1776-07-04","observation_end":"9999-12-31","units":"pca","output_type":"1","file_type":"xml","order_by":"observation_date","sort_order":"asc","count":"263","offset":"0","limit":"100000","observation":[{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"1947-01-01","value":"."},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"1947-04-01","value":"-0.60853"}]}
In the controller, the JSON is passed to an instance variable:
# Sending the http request
response = Net::HTTP.get_response(uri)
# Capturing the http response
test = response.body
# Converting the http response hash from XML to JSON
show_info = Hash.from_xml(test).to_json
@it = show_info
Finally, the instance variable is rendered in a view:
<script>
x = "<%= @it %>"
</script>
When entering "x" into the console, the JSON appears formatted differently, possibly causing issues.
{"observations":{"realtime_start":"2012-12-27","realtime_end":"2012-12-27", ... }
The question at hand is how to access specific key-value pairs? Is parsing necessary in JavaScript? Can this be done using dot notation?