rubygems

JSON

Introduction#

The json gem for Ruby allows for the parsing and creation of JSON.

Syntax#

  • JSON.parse(json_document_string) => returns a Hash of the JSON document
  • JSON.generate(ruby_hash) => returns a JSON document in the form of a String

Parameters#

Parameter Details
json_document_string A JSON document in the form of a String
ruby_hash Any Hash object
## Hash to JSON
require 'json'
data = {"test" => 123}
puts JSON.generate(data)

JSON to Hash

require 'json'
document = "{\"test\":123}"
puts JSON.parse(document)

Alternate JSON to Hash

require 'json'
data = JSON '{"test":23}'  # => {"test"=>23}

or

require 'json'
data = JSON['{"test":23}'] # => {"test"=>23}

Alternate Hash to JSON

require 'json'
document = JSON 'test'  => 23 # => "{\"test\":23}"

or

require 'json'
document = JSON['test' => 23] # => "{\"test\":23}"

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow