What is JSON and why is it important?
So then why wouldn’t we want to look at at a good looking website with engaging content? Sometimes we want to just see the raw data that is available to us from the database in the form of an array of objects that contain many attributes, otherwise known as an array of hashes.
So how can we consume JSON data in our ruby on rails app? Simple, a great tool for this would be the Unirest gem. First download the google chrome extension called JSONView, you will find the link to this below, then use the .json site that you found and use this extension.
Now that you have that dowloaded go into your rails application and include into your Gemfile
gem 'unirest', '~> 1.1.2'
Don’t forget to ‘bundle’ in the terminal!
So, lets check out what the layout for a Unirest request would look like..
response = Unirest.get(https://coolwebsite.com/path/to/content.json).body
What we are doing here is calling the Unirest gem and saying .get because we are trying a to get a web response, and using the website with the JSON content as the argument we are passing in. This will grant us access to the objects and attributes located in the database for that website. The .body at the end is giving us the array of hashes that will be displayed.
So now that we have gone over how to access JSON content why is being able to view JSON something we care about? Well there are many reasons, but one very important reason is that it’s used very widely in API web responses. So when you are implementing an API into your application the web response you are most likely to get is a JSON response. Being able to see data that you are receiving in a clear way (such as an array of hashes) is very helpful in order to manipulate that data to do what you want with it!