Latest web development tutorials

JSON syntax

JSON syntax is a subset of JavaScript syntax.


JSON syntax rules

JavaScript Object Notation JSON syntax is a subset of the French and France.

  • Data in name / value pairs
  • Data separated by commas
  • Save Object braces
  • Save the array brackets

JSON name / value pairs

Writing JSON data format is: name / value pairs.

Name / value pairs include field names (in double quotes), followed by writing a colon, then the value:

"firstName" : "John"

It is easy to understand, this is equivalent to the JavaScript statement:

firstName = "John"


JSON value

JSON values ​​can be:

  • Number (integer or floating point)
  • String (in double quotes)
  • Logical value (true or false)
  • Array (in square brackets)
  • Object (braces)
  • null

JSON object

JSON object written in curly braces:

Objects can contain multiple name / value pairs:

{ "firstName":"John" , "lastName":"Doe" }

It is also easy to understand, and this JavaScript statement is equivalent to:

firstName = "John"
lastName = "Doe"


JSON array

JSON array written in brackets:

Array can contain multiple objects:

{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

In the above example, the object "employees" is an array containing three objects. Each object on behalf of a person (name and surname) records.


JSON using JavaScript syntax

Because JSON using JavaScript syntax, so no additional software is able to handle JavaScript in JSON.

Through JavaScript, you can create an array object and assign like this:

Examples

var employees = [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName": "Jones" }
];

JavaScript can access an array of objects like the first:

employees[0].lastName;

Back reads:

Doe

You can modify the data like this:

employees[0].firstName = "Jonatan";

try it"

In the following sections, you will learn how to convert text to JSON JavaScript object.


JSON file

  • JSON file type is ".json"
  • MIME type of JSON text is "application / json"