Explaining JSON/YAML

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Explain JSON and YAML

JSON and YAML

Business scenario

Continuing the previous scenario, let's assume that your company is interested in using standardized techniques for modern web development. This includes the use of JSON (JavaScript Object Notation) and YAML (originally meaning Yet Another Markup Language) for data exchange and configuration files, for example. You need to become more familiar with these open standard formats.

What are JSON and YAML?

JSON

JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute: value pairs and arrays. It is a common data format with a diverse range of functionality in data interchange including communication of web applications with servers.

JSON is a lighter plain-text alternative to XML and based on JavaScript syntax but is independent of JavaScript and supported in other programming languages as well. JSON file names use the extension .json.

JSON is built on two structures:

  • A collection of name: value pairs. In various languages, this is realized as an object.
  • An ordered list of values. In most languages, this is realized as an array.

The following example shows a possible JSON representation describing a person.

Code snippet

{
   "firstName": "John",
   "lastName": "Smith",
   "isAlive": true,
   "age": 27,
   "address": {
     "streetAddress": "21 2nd Street",
     "city": "New York",
     "state": "NY", "postalCode": "10021-3100"
   },
 "phoneNumbers": [
   {
     "type": "home",
     "number": "212 55-1234"
   },
   {
     "type": "office",
     "number": "646 555-4567"
   }
  ],
  "children": [],
  "spouse": null
 }
Copy code

If you currently don’t have an editor installed supporting JSON, here is what it looks like with syntax highlighting.

YAML

YAML is a human-friendly, cross language, Unicode based data serialization language designed around the common native data types of agile programming languages. It is broadly useful for programming needs ranging from configuration files to internet messaging to object persistence to data auditing.

YAML was specifically created to work well for common use cases such as: configuration files, log files, interprocess messaging, cross-language data sharing, object persistence, and debugging of complex data structures. When data is easy to view and understand, programming becomes a simpler task. YAML filenames use the extension .yaml or .yml.

YAML is a strict JSON superset and includes additional features such as the notion of tagging data types, support for non-hierarchical data structures, the option to structure data with indentation, and multiple forms of scalar data quoting. YAML is an open format.

The following example shows a possible YAML representation describing a family.

Code snippet
--- # The Smiths
 - { name: John Smith, age: 33 }
 - name: Mary Smith
   age: 27
 - [name, age]: [Rae Smith, 4] # sequences as keys are supported
 --- # People, by gender
 men: [John Smith, Bill Jones]
 women:
   - Mary Smith
   - Susan Williams
Copy code

If you currently don’t have an editor installed supporting YAML, here is what it looks like with syntax highlighting.

Objects and lists are important components in YAML and can be mixed. The first example is a list of key-value objects, all people from the Smith family. The second lists them by gender; it is a key-value object containing two lists.

YAML in Relation to JSON

Both JSON and YAML aim to be human-readable data interchange formats. However, JSON and YAML have different priorities.

YAML can therefore be viewed as a natural superset of JSON, offering improved human readability and a more complete information model. This is also the case in practice; every JSON file is also a valid YAML file. This makes it easy to migrate from JSON to YAML if or when the additional features are required.

Summary

You now have a more profound understanding of JSON and YAML.

Save progress to your learning plan by logging in or creating an account