I would like to produce JSON formatted like this:
{
"a": {
"b": 1
},
"c": [
2
],
"d": [],
"e": {}
}
This is easy to do with the standard JSON support in Python, JavaScript, and Go.
In Ruby, I'm having trouble getting that exact output:
$ ~/.rbenv/versions/2.6.3/bin/irb
irb(main):001:0> require 'json'
=> true
irb(main):002:0> puts JSON.pretty_generate({"a" => {"b" => 1}, "c" => [2], "d" => [], "e" => {}})
{
"a": {
"b": 1
},
"c": [
2
],
"d": [
],
"e": {
}
}
=> nil
The problems:
- Empty objects are rendered on two lines.
- Empty arrays are rendered on three(!) lines.
Is there a simple way to get the Ruby standard JSON support to render the output I want?