Cover by Lorna Jane Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Chapter 7. JSON

JSON stands for JavaScript Object Notation, but don’t be fooled by the name. Although it sounds as if it’s a JavaScript-specific format, it is easily read and writeable by a wide range of scripting languages today. It’s a very simple, lightweight format which can represent nested, structured data.

For example, if we had data that looked like this:

  • message

    • en: “hello friend”
    • es: “hola amigo”

In JSON, that data would look like this:

{"message":{"en":"hello friend","es":"hola amigo"}}

If a piece of data is a scalar value, then it is presented plainly. If it is structured like that shown above, in PHP something like an associative array or an object with properties, we use a curly brace to indicate a new level of depth in the data structure. The keys and values are separated by colons, and each record at a given level is separated with a comma.

It is also possible to show a list of items quite elegantly using JSON. Take this imaginary simple shopping list:

  • eggs
  • bread
  • milk
  • bananas
  • bacon
  • cheese

A JSON representation of this would simply be:

["eggs","bread","milk","bananas","bacon","cheese"]

As you can see here, the keys we had in the previous example are optional, and where we have multiple values, these are enclosed with the simple square brackets. If this list was in fact the value of a property, then we’d see both kinds of brackets:

{"list":["eggs","bread","milk","bananas","bacon","cheese"]}

This example shows that our data contained a key/value pair, with the key “list”. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required