Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSON syntax


May 10, 2021 Java


Table of contents


Java JSON Tutorial - JSON Syntax


The JSON syntax is a subset of the JavaScript object representing the syntax.

  • The data is represented by a name/value pair.
  • The data is separated by a comma
  • The parenthesis encloses the object, and the name/value pair is separated , .
  • Square brackets hold the array, and the values are , by .

JSON supports two data structures:

Data Describe
A collection of name/value pairs key:value,key:value,
An ordered list of values [1,2,3,4]


JSON value

In JSON, the value must be one of the following data types:

  • String (in double quotes)
  • Numbers (integers or floats)
  • Object (in braces)
  • Array (in parentheses)
  • Logical value (true or false)
  • Null

In JavaScript, all listed above can be values, plus other valid JavaScript expressions, including:

  • Function
  • Date
  • undefined

In JSON, string values must be written in double quotes

Example

JSON data - name and value

JSON data is written as a name/value pair.

The name/value consists of a field name, followed by a colon and a value:

Instance

"name":"W3Cschool"

Equivalent to:

name = "W3Cschool"

The JSON name requires double quotes. The JavaScript name is not required.

JSON - The value is a JavaScript object

The JSON format is almost identical to JavaScript objects.

In JSON, the key must be a string, surrounded by double quotes:

Json

{ "name":"W3Cschool" }

In JavaScript, the key can be a string, number, or identifier name:

Javascript

{ name:"W3Cschool" }

Example

The following JSON data specifies the book.

{
    "book": [
    {
       "id":"01",
       "language": "Java",
       "edition": "third",
       "author": "java2s.com"
    },
    {
       "id":"02",
       "language": "JSON",
       "edition": "second"
       "author": "java2s.com"
    }]
}