Reputation: 6761
Often I find myself in the situation where I start exploring a new API and get back large JSON objects. And everytime I struggle to find a way to display them properly indented, so that I can easily see the structure and identify the parts relevant to my task.
Right now I am exploring the Z-Wave API for Razberry (Home Automation). I get back a JSON holding all the devices connected to the Home Automation network. It looks like this:
And I don't understand why Rails breaks the lines in the way it can be seen here, and why it doesn't manage to indent the JSON data properly. The output on the screenshot is the result of this controller code:
request = Net::HTTP::Get.new(uri)
request.basic_auth 'admin', 'bla'
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end
@devices = JSON.parse(response.body)
render json: @devices
It would be interesting to see how more experienced programmers handle JSON at this stage, when exploring the datastructure. How can I display the JSON in this style?:
{"1":
{"instances":
{"0":{
"commandClasses":
{"32":
{"name":"Basic",
"data":
{"invalidateTime":1462455293,
"updateTime":1462365990,
"type":"empty",
"value":null,
"supported":
{"invalidateTime":1462455293,
"updateTime":1462365990,
"type":"bool",
"value":true},
"version":
Upvotes: 0
Views: 526
Reputation: 4970
Assuming you are willing to broaden the question to 'what can I do to make the output of my response body as human friendly as possible?', I'd like to also suggest using Awesome Print. Requiring 'awesome_print' results in the instance method ai
(for awesome inspect) being added to Kernel
. It can take various arguments, or none at all, such as in the irb session pasted below:
2.3.0 :030 > require 'awesome_print'
=> false
2.3.0 :031 > response_body.ai(html: true)
=> "<pre>{\n "array"<kbd style=\"color:slategray\"> => </kbd>[\n <kbd style=\"color:white\">[0] </kbd><kbd style=\"color:blue\">1</kbd>,\n <kbd style=\"color:white\">[1] </kbd><kbd style=\"color:blue\">2</kbd>,\n <kbd style=\"color:white\">[2] </kbd><kbd style=\"color:blue\">3</kbd>,\n <kbd style=\"color:white\">[3] </kbd>{\n "sample"<kbd style=\"color:slategray\"> => </kbd><kbd style=\"color:brown\">"hash"</kbd>\n }\n ],\n "foo"<kbd style=\"color:slategray\"> => </kbd><kbd style=\"color:brown\">"bar"</kbd>\n}</pre>"
2.3.0 :032 > puts response_body.ai
{
"array" => [
[0] 1,
[1] 2,
[2] 3,
[3] {
"sample" => "hash"
}
],
"foo" => "bar"
}
=> nil
It's so easy in Ruby to support JSON, YAML, and Awesome Print, that I offer all 3 in my code.
Upvotes: 1
Reputation: 44370
You can use JSON.pretty_generate()
method:
Example:
require 'json'
my_json = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_json)
Output:
{
"array": [
1,
2,
3,
{
"sample": "hash"
}
],
"foo": "bar"
}
Your code:
request = Net::HTTP::Get.new(uri)
request.basic_auth 'admin', 'bla'
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end
@devices = JSON.parse(response.body)
render json: JSON.pretty_generate(@devices)
Upvotes: 1