Skip to content
instance method

JSON#pretty_generate

(::_ToJson obj, ?::JSON::options opts) -> untyped
JSON.pretty_generate(obj, opts = nil) → new_string

Arguments obj and opts here are the same as arguments obj and opts in JSON.generate.

Default options are:

Example
{
  indent: '  ',   # Two spaces
  space: ' ',     # One space
  array_nl: "\n", # Newline
  object_nl: "\n" # Newline
}

Example:

Example
obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
json = JSON.pretty_generate(obj)
puts json

Output:

Example
{
  "foo": [
    "bar",
    "baz"
  ],
  "bat": {
    "bam": 0,
    "bad": 1
  }
}
See also #parse! #unsafe_load #parse also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0