English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSON Syntax

JSON objects are names/Value (name:value) data format, usually displayed in curly braces.

A JSON object is as follows:

  {
  "name": "Seagull",
  "age": 22,
  "city": "New Delhi",
  }

When using JSON, you may see JSON objects in .json files, but they can also exist as JSON objects or strings in the program context.

JSON syntax rules

JSON syntax is basically considered a subset of JavaScript syntax, including the following:

  • data is named/represented in the form of value pairs

  • data is separated by commas

  • curly braces support objects

  • brackets can contain arrays

JSON structure

JSON is built on two structures:

  • Name/a collection of value pairs (objects)

  • an ordered list (array) of values

JSON data-Name/value pairs

JSON data is named/represented in the form of value pairs.

Name/Value pairs are composed of a field name (enclosed in double quotes), a colon, and a value:

  "name":"Seagull"

JSON names require double quotes. JavaScript names do not.

JSON-Evaluates to JavaScript object

JSON format is almost identical to JavaScript objects.

In JSON, keys must be strings and enclosed in double quotes:

  {"name":"Seagull"}

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

  {"name":"Seagull"}

In JSON, string values must be enclosed in double quotes:

  {"name":"Seagull"}

In JavaScript, you can write string values with double quotes or single quotes:

  {"name":"Seagull"}

JSON vs XML

JSON and XML can both be used to receive data from web servers.

Both the JSON and XML examples below define a book object that contains3This book's array:

JSON Example:
  {"books":[
  {"author":"Bella","title":"XML Developer's Guide"},
  {"author":"Kim","title":"Midnight Rain"},
  {"author":"Cynthia","title":"Lover Birds"}
  }]
XML Example:
  <books>
  <book>
    <author>Bella</author>/<title>XML Developer's Guide</title>/title>
  </book>
  <book>
    <author>Kim</author>/<title>Midnight Rain</title>/title>
  </book>
  <book>
    <author>Cynthia</author>/<title>Lover Birds</title>/title>
  </book>
  </books>

From the above examples, you can see:

  • JSON does not use end tags

  • JSON is shorter

  • JSON read and write is faster

  • JSON can use arrays

The biggest difference is:XML must be parsed using an XML parser. JSON can be parsed using standard JavaScript functions.

JSON is better than XML:

  • XML is much more difficult to parse than JSON

  • JSON is parsed into a ready-to-use JavaScript object