Human-Readable Data Serialization (json)
JavaScript Object Notation (JSON) is an object serialization format that has been gaining a ton of steam lately. With the rise of a service-oriented Web, the need for a simple, language-independent data serialization format has become more and more apparent.
Historically, XML has been used for interoperable data serialization. However, using XML for this is a bit like going bird hunting with a bazooka: it’s just way more firepower than the job requires. JSON aims to do one thing and do it well, and these constraints give rise to a human-readable, human-editable, easy-to-parse, and easy-to-produce data interchange format.
The primitive constructs provided by JSON are limited to hashes
(called objects in JSON), arrays, strings, numbers,
and the conditional triumvirate of true, false,
and nil (called null in JSON). It’s easy to see that these
concepts trivially map to core Ruby objects. Let’s take a moment to see
how each of these data types are represented in JSON:
require "json"
hash = { "Foo" => [Math::PI, 1, "kittens"],
"Bar" => [false, nil, true], "Baz" => { "X" => "Y" } } #...
puts hash.to_json #=> Outputs
{"Foo":[3.14159265358979,1,"kittens"],"Bar":[false,null,true],"Baz":{"X":"Y"}}There isn’t really much to it. In fact, JSON is somewhat syntactically similar to Ruby. Though the similarity is only superficial, it is nice to be able to read and write structures in a format that doesn’t feel completely alien.
If we go the other direction, from ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access