Site icon

JSON Syntax

JSON Syntax Basics:

Employee Object
{
  "First Name" : "Mike",
  "Last Name" : "harvey",
  "Age" : 34,
  "Country" : "USA"
}

JSON Data Types:

String:

A string in JSON represents a sequence of characters enclosed in double quotes. It can contain letters, numbers, symbols, and special characters. For example:

{
  "name": "John Lagaas",
  "location": "Amsterdam",
  "profession": "Lawyer"
}

Number:

JSON supports numeric values, including integers and floating-point numbers. Numbers in JSON do not require quotes and can be expressed as whole numbers or with decimal points.

{
  "employeeID": 12345,
  "salary": 75000
}

Array:

Arrays in JSON are ordered lists of values enclosed in square brackets []. They can contain multiple values of different data types, separated by commas. For example: [“JavaScript”, “Python”, “SQL”]

{ 
"skills": ["JavaScript", "Python", "SQL"]
}

Boolean

Boolean values in JSON represent true or false. They are not enclosed in quotes. For example: true or false

{
  "isWorking": true,
  "citizen": false
}

Null

In JSON, null represents the absence of a value or the explicit indication of no data. It is not enclosed in quotes. For example: null.

{
"address": null
}

Object

JSON objects are collections of key-value pairs enclosed in curly braces {}. Each key is a string enclosed in quotes, followed by a colon and its corresponding value. For example:

{
  "name": "John Lagaas",
  "location": "Amsterdam",
  "profession": "Lawyer",
  "age": 35,
  "isLicensed": true
}

Example Representing all data types of JSON

{
  "name": "John Lagaas",
  "location": "Amsterdam",
  "profession": "Lawyer",
  "age": 35,
  "isLicensed": true,
  "specializations": ["Corporate Law", "Intellectual Property"],
  "contact": {
    "email": "john@example.com",
    "phone": "+1234567890"
  },
  "cases": [
    {
      "caseNumber": "ABC123",
      "status": "Ongoing"
    },
    {
      "caseNumber": "XYZ456",
      "status": "Closed"
    }
  ]
}

Importance of Commas:

Separator Between Elements:

Commas , are used to separate individual elements within a JSON object or array. They act as separators between key-value pairs in objects and between elements in arrays.

Syntax Requirement:

Commas are a fundamental part of the JSON syntax. Incorrect placement or missing commas can result in syntax errors and cause the JSON data to be invalid.

Example: In a JSON object { "key1": "value1", "key2": "value2" }, the comma separates the key-value pairs ("key1": "value1" and "key2": "value2"), allowing multiple pairs to exist within the same object.

Importance of Curly Braces {}:

Role in JSON Structure:

Exit mobile version