JSON Syntax Basics:
- JSON consists of key-value pairs.
- Data is represented as attribute-value pairs
- separated by a colon (
:
), and different pairs are separated by commas (,
).
- separated by a colon (
- JSON objects are enclosed in curly braces
{}
, and arrays are enclosed in square brackets[]
. - Example of a simple JSON object:
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 {}
:
- Defining Objects: Curly braces
{}
are used to encapsulate a collection of key-value pairs within a JSON object. They define the beginning and end of an object. - Structuring Data: Objects in JSON allow for organizing data hierarchically, where each key represents a property and its associated value.
- Example:
{ "name": "John", "age": 30, "isStudent": true }
represents a JSON object with three key-value pairs encapsulated within curly braces.
Role in JSON Structure:
- Objects vs. Arrays: Curly braces
{}
define JSON objects, providing a way to structure data with named keys and corresponding values. Arrays, on the other hand, use square brackets[]
and contain ordered lists of values. - Clarity and Readability: Commas and curly braces contribute to the readability of JSON data by establishing clear boundaries between elements and delineating the structure.