Ruby Monstas


Introduction to JSON

JSON

JSON logo

JavaScript Object Notation

What is it?

JSON is

  • a lightweight data-interchange format.
  • easy for humans to read and write.
  • easy for machines to parse and generate.

Source https://www.json.org/

Why JSON?

JSON is a text format that is completely programming language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

These properties make JSON an ideal data-interchange language.

Source https://www.json.org/

Example

This is an example of a JSON object

{
  "name": "Matt Damon",
  "bornInYear": 1970,
  "academyAwards": null,
  "appearsIn": ["Ocean's 11", "Invictus", "The Martian"]
}

JSON object / Ruby Hash

At first glance, a JSON object looks very similar to a Ruby Hash, but there are some major differences:

  • JSON is just a text document.
  • Keys are enclosed by "double quotes".
  • Keys are written in camelCase.
  • Empty values are described as
    null

JSON is just a text document.

It's very crucial to understand, that JSON is just a text document.

With JSON itself we cannot do anything programmatically.

So, even if the syntax looks similar to a Ruby Hash, they're actually different.

Anyway, following slides describe some syntactic differences between a JSON object and a Ruby Hash.

Keys are enclosed by "double quotes".

JSON
JSON logo
Ruby
Ruby logo
{ "city": "Sydney" }
{ city: "Sydney" }
Keys in JSON objects are enclosed by "double quotes".
Keys in Ruby Hashes don't need quotes.

Keys are written in camelCase.

JSON
JSON logo
Ruby
Ruby logo
{ "dateOfBirth": "1970-10-08" }
{ date_of_birth: "1970-10-08" }
Keys in JSON objects are written in camelCase.
Keys in Ruby Hashes are written in snake_case.

null
vs
nil

JSON
JSON logo
Ruby
Ruby logo
{ "academyAwards": null }
{ academy_awards: nil }
The placeholder value for "nothing" is
null
in JSON and
nil
in Ruby.

JSON is built on two basic structures

  • A collection of name/value pairs.
  • An ordered list of values.

Using these two structures we can describe any kind of data.

Source https://www.json.org/

A collection of name/value pairs.

JSON
JSON logo
Ruby
Ruby logo
{
  "animal": "Elephant",
  "weightInKg": 703.5,
  "lovesMusic": true,
  "injuries": null,
  "caretakers": [
    "Judy", "Tom"
  ],
  "location": {
    "sector": "East",
    "enclosure": "7A"
  }
}
{
  animal: "Elephant",
  weight_in_kg: 703.5,
  lovesMusic: true,
  injuries: nil,
  caretakers: [
    "Judy", "Tom"
  ],
  location: {
    sector: "East",
    enclosure: "7A"
  }
}
Note that values can be Arrays (see
caretakers
) or Objects (see
location
) again.

An ordered list of values.

JSON
JSON logo
Ruby
Ruby logo
["red", "blue", "green"]
[1, 1, 2, 3, 5, 8]
[false, true, null]
["red", "blue", "green"]
[1, 1, 2, 3, 5, 8]
[false, true, nil]
...

An ordered list of values.

JSON
JSON logo
Ruby
Ruby logo
[
  ["GER", "SUI"]
  ["ARG", "BRA"],
  ["POR", "ESP"],
]
[
  { "name": "Greta" },
  { "name": "Jacinda" },
  { "name": "Angela" },
]
[
  ["GER", "SUI"]
  ["ARG", "BRA"],
  ["POR", "ESP"],
]
[
  { name: "Greta" },
  { name: "Jacinda" },
  { name: "Angela" },
]
For Arrays there's no difference between JSON and Ruby.
Again, we can also nest Arrays and/or Objects within Arrays.

Additional Resources

Official JSON documentation
JSON article on Wikipedia

What questions do you have?