JSON (JavaScript Object Notation) is most widely used data format for data interchange on the web. This data interchange can happen between two computers applications at different geographical locations or running within same hardware machine.

The good thing is that JSON is a human and machine readable format. So while applications/libraries can parse the JSON data – humans can also look at data and derive meaning from it.

A JSON document may contains text, curly braces, square brackets, colons, commas, double quotes, and maybe a few other characters.

Primarily, JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

We are using JSON sytnax in our Rest APIs.

JSON Syntax

A JSON document may contain information separated by following separators/token.

  • : to separate name from value
  • , to separate name-value pairs
  • { and } for objects
  • [ and ] for arrays

JSON name-value pairs example

Name-value pairs have a colon between them as in name : value.

JSON names are on the left side of the colon. They need to be wrapped in double quotation marks, as in name, and can be any valid string. Within each object, keys need to be unique.

JSON values are found to the right of the colon. At the granular level, these need to be one of 6 simple data types:

  • strings
  • numbers
  • objects
  • arrays
  • booleans
  • null or empty

Each name-value pair is separated by a comma, so the JSON looks like this:

"name" : "value", "name" : "value", "name": "value"
{
  "color" : "Purple",
  "id" : "210"
}

JSON object example

A JSON object is a key-value data format that is typically rendered in curly braces. A JSON object looks something like this:

{
  "color" : "Purple",
  "id" : "210",
  "composition" : {
    "R" : 70,
    "G" : 39,
    "B" : 89
  }
}

JSON array example

Data can also be nested within the JSON by using JavaScript arrays that are passed as a value using square brackets [ ] on either end of its array type.

JSON arrays are ordered collections and can contain values of differing data types.

{
    "colors" :
    [
        {
          "color" : "Purple",
          "id" : "210"
        },
        {
          "color" : "Blue",
          "id" : "211"
        },
        {
          "color" : "Black",
          "id" : "212"
        }
    ]
}

JSON Data Types

JSON consist of 6 data types. First four data types (string, number, boolean and null) can be referred as simple data types. Other two data types (object and array) can be referred as complex data types.

  • string
  • number
  • boolean
  • null/empty
  • object
  • array
Data Type Description
string Strings in JSON must be written in double quotes.

{
  "color" : "Purple"
}
number Numbers in JSON must be an integer or a floating point.

{
  "number_1": 210,
  "number_2": -210,
  "number_3": 21.05,
  "number_4": 1.0E+2
}
boolean Value can be either true or false.

{
  "visibility": true
}
null Values in JSON can be null.

{
   "middlename": null
}
object Values in JSON can be objects.

"employee": {
  "name": "John",
  "age": 30,
  "city": "New York"
}
array Values in JSON can be arrays.

{
  "employees": [ "John", "Anna", "Peter" ]
}

Methods

Name Description
encode() Returns the JSON representation of a value.
decode() Takes a JSON encoded string and converts it into a PHP variable.

Methods Details

encode()

Returns the JSON representation of a value

Examples

$data = [
                'title' => 'Revelations',
                'description' => 'While still dealing with the loss of Daniel Jackson the SGC is contacted by the Asgard who require assistance dealing with Anubis, who seems to have new shield technology that can repel Asgard weapons.',
                'director' => 'Martin Wood',
                'writers' => 'Brad Wright, Jonathan Glassner',
                'stars' => 'Richard Dean Anderson, Michael Shanks, Amanda Tapping',
                'content' => 'Osiris engages in space combat with Thor over a violation of the protected planets treaty. Freyr arrives at the SGC bringing news of Thor\'s death and asking SG-1 to mount a rescue mission to retrieve an Asgard scientist from the planet in question. Upon their arrival Heimdall informs them that Thor still lives and has been taken captive by the Goa\'uld. O\'Neill and Teal\'c transport over to the mothership to rescue him from the clutches of Anubis.'
        ];

$data = flextype('serializers')->json()->encode($data);

decode()

Takes a JSON encoded string and converts it into a PHP variable.

Examples

$json = '{
  "title": "Meridian",
  "description": "As Jackson suffers from a fatal dose of radiation, he struggles with the value of his life while his friends deal with the emotional and diplomatic repercussions.",
  "director": "William Waring",
  "writers": "Brad Wright, Jonathan Glassner",
  "stars": "Richard Dean Anderson, Michael Shanks, Amanda Tapping",
  "content": "SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson suffering from what is likely a fatal dose of radiation. On the planet, they dealt with the country of Kelowna and their representative Jonas Quinn. That country was at the same stage of development as the United States in the 1940s and well on their way to creating an atomic weapon using Goa\'uld technology found in an ancient temple. Daniel argued against the Kelownans developing such a weapon and is accused of attempting to sabotage the project. As members of the team sit by his deathbed, Daniel receives an unexpected offer from someone they once met off-world."}'

$data = flextype('serializers')->json()->decode($json);