Chakra7 Central: Your Chakra7 Headquarters


JSON = JavaScript Object Notation

Serialized JaveScript Objects

A Primer to JSON, JavaScript Object Notation

JSON has quickly displaced XML over the past few years, primarily due to the rise of internet technologies. JSON stands for JavaScript Object Notation and is a standard data serialization method for representing arbitrarily complex data(JavaScript Object) as a series of characters (JSON String)

JavaScript objects are pieces of data that can hold a number of key/ value pairs known as properties and property values. It is useful to start any introduction to JSON with the building block object types(numbers, strings, arrays, and freeform objects) that can be combined and nested to describe almost any data, regardless of its complexity.

The following are supported JSON types:

1. null (Not Defined)

Represents not defined or no value:

JavaScript Code:
var numberObj = null;
console.log(JSON.stringify(nullObj)); //Prints "null"

PHP Code:
$numberObj = null;
echo json_encode($numberObj); //Prints "null"

2. Boolean (True or False)

True or False

JavaScript Code:
var boolObj = true;
console.log(JSON.stringify(boolObj)); //Prints "true"

PHP Code:
$stringObj = false;
echo json_encode($boolObj); //Prints "false"

3. Numbers

Either whole or decimal numeric types:

Examples: 7 and 13.27

JavaScript Code:
var numberObj = 17;
console.log(JSON.stringify(numberObj)); //Prints 17

PHP Code:
$numberObj = 17;
echo json_encode($numberObj); //Prints 17

4. Strings

A sequence of characters used to store text data.

Examples: "Tom" and "Bob Dylan". Notice the syntax in describing a string in JSON: simply surrounding the text phrase with double quotation marks defines a string object

JavaScript Code:
var stringObj = "John Lennon";
console.log(JSON.stringify(stringObj)); //Prints "John Lennon"

PHP Code:
$stringObj = "John Lennon";
echo json_encode($stringObj); //Prints "John Lennon"

5. Arrays

An ordered sequence of JSON objects

Examples include [1, 2, 3, 4, 5] and ["Blowin", "In", "The", "Wind"]. The syntax in describing a string in JSON is simply surrounding the text phrase with double quotation marks.

JavaScript Code:
var arrayObj = ["John", "Paul", "George"];
console.log(JSON.stringify(arrayObj)); //Prints: ["John", "Paul", "George", "Ringo"]

PHP Code:
$arrayObj = array("John", "Paul", "George");
$arrayObj[] = "Ringo";
echo json_encode($arrayObj); //Prints: ["John", "Paul", "George", "Ringo"]

6. Objects

A collection of key/ value pairs that define properties

Examples include {} and {"fName": "John", "lName":"Lennon"}. The properties are defined in comma-separated property:value format and surrounded by curly braces. {} represents an empty object with no properties defined

JavaScript Code:
var obj = {
"n":"The Rolling Stones",
"firstHitSong":"(I Can’t Get No) Satisfaction"
//Prints: "{"n":"The Rolling Stones","yearHit":1965,"firstHitSong":"(I Can’t Get No) Satisfaction"}"

PHP Code:
$obj = array(
"n"=>"The Rolling Stones",
"firstHitSong"=>"(I Can’t Get No) Satisfaction"
echo json_encode($obj);
//Prints: "{"n":"The Rolling Stones","yearHit":1965,"firstHitSong":"(I Can’t Get No) Satisfaction"}"

A Multi-Language Example: JavaScript PHP


var TheBeatles = {
"artist":"The Beatles",
"firstHit": {
"title":"Love Me Do",
"lastHit": {
"title":"The Long And Winding Road",
"album":"Let It Be"

"{"artist":"The Beatles","firstHit":{"title":"Love Me Do","single":true,"year":1966},"lastHit":{"title":"The Long And Winding Road","single":false,"album":"Let It Be"}}"


(Assumes that we've AJAXed the Artist JSON string to the server)
function getFirstHitTitle(artistJSONString) {
$artistObj = json_decode(artistJSONString, true);
if(isset($artistObj["firstHit"])) {
echo "First hit for " . $artistObj["artist"] . " is " . $artistObj["firstHit"]["title"];
} else {
echo "No First Hit";

//First hit for The Beatles is Love Me Do