JSON

Other topics

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}"

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:

ParameterDetails
json_document_stringA JSON document in the form of a String
ruby_hashAny Hash object

Contributors

Topic Id: 8894

Example Ids: 27694,27695,27696,27697

This site is not affiliated with any of the contributors.