[RAILS] Convert ruby object to JSON format

It puts a ruby object such as a hash or an array in the argument and returns it in JSON format.


pry(main)> JSON.generate({"hoge"=>{"fuga"=>1, "fugafuga"=>2}})
=> "{\"hoge\":{\"fuga\":1,\"fugafuga\":2}}"


pry(main)> JSON.pretty_generate({"hoge"=>{"fuga"=>1, "fugafuga"=>2}})
=> "{\n  \"hoge\": {\n    \"fuga\": 1,\n    \"fugafuga\": 2\n  }\n}"

It returns a string that is easier to read than JSON.generate.

It was said that it is better to use generate because generate is lighter in capacity unless it is read by humans.

Recommended Posts

Convert ruby object to JSON format
Convert JSON to TSV and TSV to JSON with Ruby
Convert to Ruby Leet string
Convert Serializable Object to byte []
Convert numbers to Roman numerals in Ruby
Mapping json to an already instantiated Object
[Java] Convert Object type null to String type
Introduction to Ruby 2
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Ruby] How to convert CSV file to Yaml (Yml)
[Ruby] "Reference to object" and "Contents of variable"
Convert Json to List <T> as it is
Convert Java nested beans to aaa.bbb [0] .ccc format
Ruby How to convert between uppercase and lowercase
Convert Java enum enums and JSON to and from Jackson
Ruby Learning # 31 Object Methods
From Java to Ruby !!
Introduce RMagick to convert existing existing image files to another format
[Kotlin] Convert ZonedDateTime to String by specifying the format
Format JSON with org.json
[Docker] Convert yml ⇆ json
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
19 Corresponds to object creation
[Easy] How to automatically format Ruby erb file with vsCode
Compared processing time to convert Apache Arrow and JSON to Yosegi
Serializing org.joda.time.DateTime to JSON in ISO 8601 format comes with milliseconds
Convert 2D array to csv format with Java 8 Stream API
[Ruby] How to batch convert strings in an array to numbers
Convert Java Powerpoint to XPS
I want to convert characters ...
Introduction to Ratpack (5) --Json & Registry
Convert String type to Timestamp type
How to use Ruby return
Explanation about Ruby Range object
18 Corresponds to JSON-like object definitions
Format Ruby with VS Code
[Ruby] How to comment out
[Ruby] Introduction to Ruby Error statement
Convert from ○ months to ○ years ○ months
Rbenv command to use Ruby
Ruby: How to use cookies
[Ruby] How to write blocks
How to convert Java radix
About Ruby and object model
[Java] Convert ArrayList to array
[Jackson] JSON ⇔ object conversion class
Explanation about Ruby String object
[Ruby] I want to put an array in a variable. I want to convert to an array
[Android] Convert Map to JSON using GSON in Kotlin and Java