logo

JSON Syntax


Show

The JSON syntax is a subset of the JavaScript syntax.

JSON Syntax Rules

JSON syntax is derived from JavaScript item notation syntax:

  • Data is in name/value pairs
  • Data is separated through commas
  • Curly braces keep objects
  • Square brackets keep arrays

JSON Data - A Name and a Value

JSON information is written as name/value pairs (aka key/value pairs).

A name/value pair includes a field name (in double quotes), accompanied through a colon, followed through a value:

Example

"name":"John"

JSON- Evaluates to JavaScript Objects

The JSON formation is almost similar to JavaScript objects.

In JSON, keys should be strings, and written with double quotes:

JSON

{"name":"John"}

In JavaScript, keys might be strings, numbers, or identifier names:

JavaScript

{name:"John"}

JSON Values

In JSON, values should be one of the following data types:

  • a string
  • a number
  • an object
  • an array
  • a boolean
  • null

In JavaScript, values can be all of the above, plus any other valid JavaScript expression, including:

  • a function
  • a date
  • undefined

In JSON, string value must be written with double quotes:

JSON

{"name":"John"}

In JavaScript, you can note string values with double and single quotes:

JavaScript

{name:'John'}

A simple Example is Given Below;

{
   "book": [

      {
         "id": "01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      },

      {
         "id": "07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy"
      }

   ]
}

JSON hold up the following two data structures −

  • Collection of name/value pairs − This Data Structure is carried and supported by different programming languages.
  • The ordered list of values − It comprises array, list, vector or sequence, etc.

JavaScript Array As JSON

A similar method of JavaScript objects can be written as JSON, JavaScript arrays can also be written as JSON.

You will get to learn more about objects and arrays in this tutorial after some time.

JSON Files

  • The file type for JSON files is ".json"
  • The MIME type for JSON text is "application/json"