JSON Syntax - Complete Guide

Master JSON syntax rules, structure, and best practices

Published: January 2025 • 12 min read

JSON (JavaScript Object Notation) follows strict syntax rules that make it both human-readable and machine-parsable. Understanding JSON syntax is essential for working with APIs, configuration files, and data exchange between systems.

This comprehensive guide covers every aspect of JSON syntax, from basic rules to advanced structures. Whether you're learning what JSON is or debugging syntax errors, this guide will help you write valid JSON every time.

Quick Tip: Use our JSON Validator to check your JSON syntax and identify errors immediately.

Basic JSON Syntax Rules

JSON syntax is based on a subset of JavaScript, but it's stricter. Here are the fundamental rules you must follow:

1Data is in Name/Value Pairs

JSON data is written as key-value pairs separated by a colon:

JSON
"deviceName": "Router-01"

2Data is Separated by Commas

Multiple pairs are separated by commas (but NO trailing comma):

JSON
{
  "deviceName": "Router-01",
  "port": 8080,
  "location": "DataCenter-A"
}

3Curly Braces Hold Objects

Objects are enclosed in curly braces { }:

JSON
{ "deviceName": "Router-01", "ipAddress": "192.168.1.1" }

4Square Brackets Hold Arrays

Arrays are enclosed in square brackets [ ]:

JSON
{ "interfaces": ["eth0", "eth1", "eth2"] }

Learn more about JSON arrays.

String Syntax Rules

Strings in JSON have specific requirements that differ from JavaScript:

✓ Must Use Double Quotes

Strings MUST be enclosed in double quotes, never single quotes:

Valid:

JSON
"Router-Core-01"

Invalid:

JSON
'Router-Core-01'

✓ Property Names Must Be Quoted

Unlike JavaScript, property names must be strings in double quotes:

Valid:

JSON
{ "deviceName": "Router-01" }

Invalid:

JSON
{ deviceName: "Router-01" }

✓ Escape Special Characters

Use backslash to escape special characters:

JSON
{
  "message": "Device says \"Ready\"",
  "path": "C:\\Network\\Config",
  "log": "Status: OK\nUptime: 99.9%",
  "data": "IP\tPort\tStatus"
}

Common escape sequences: \\ \" \n \t \r \b \f

JSON Data Types

JSON supports six data types. Understanding their syntax is crucial:

1. String

Text in double quotes:

JSON
{
  "deviceName": "Core-Router-01",
  "model": "Cisco ASR 9000",
  "location": "DataCenter-Primary"
}

2. Number

Integer or floating-point, NO quotes:

JSON
{
  "port": 8080,
  "latency": 2.5,
  "temperature": -5.5,
  "bandwidth": 1.5e9
}

Numbers can be negative, decimal, or in scientific notation

3. Boolean

Lowercase true or false, NO quotes:

JSON
{
  "isActive": true,
  "isOnline": false,
  "isSecure": true
}

⚠NOT "True", "TRUE", or "False" - must be lowercase!

4. Null

Represents no value, lowercase null:

JSON
{
  "backupRouter": null,
  "failoverIP": null
}

5. Object

Collection of key-value pairs in curly braces:

JSON
{
  "location": {
    "datacenter": "DC-01",
    "rack": "A-12",
    "zone": "Production"
  }
}

6. Array

Ordered list in square brackets, can contain any type:

JSON
{
  "ports": [8080, 8081, 8082, 8083],
  "devices": ["Router-01", "Switch-02", "Firewall-03"],
  "mixed": ["192.168.1.1", 443, true, null, {"type": "gateway"}]
}

Common JSON Syntax Errors

Avoid these frequent mistakes that cause JSON parsing to fail:

Error 1: Trailing Commas

Wrong:

JSON
{
  "deviceName": "Router-01",
  "port": 8080,
}

Correct:

JSON
{
  "deviceName": "Router-01",
  "port": 8080
}

Last item cannot have a comma after it

Error 2: Single Quotes

Wrong:

JSON
{ 'deviceName': 'Router-01' }

Correct:

JSON
{ "deviceName": "Router-01" }

Must use double quotes, not single

Error 3: Unquoted Property Names

Wrong:

JSON
{ deviceName: "Router-01" }

Correct:

JSON
{ "deviceName": "Router-01" }

Property names must be quoted strings

Error 4: Comments

Wrong:

JSON
{
  // This is a comment
  "deviceName": "Router-01"
}

Correct:

JSON
{
  "deviceName": "Router-01"
}

JSON does not support comments

Error 5: Undefined Values

Wrong:

JSON
{ "status": undefined }

Correct:

JSON
{ "status": null }

Use null instead of undefined

Whitespace and Formatting

JSON is flexible with whitespace, which makes formatting easy:

Whitespace is Ignored

Spaces, tabs, and newlines outside strings are ignored. These are equivalent:

Minified:

JSON
{"deviceName":"Router-01","port":8080}

Formatted:

JSON
{
  "deviceName": "Router-01",
  "port": 8080
}

Use our JSON Formatter to beautify or JSON Minify to compress.

Valid vs Invalid JSON Examples

Valid JSON

JSON
{
  "networkDevice": {
    "deviceId": 12345,
    "hostname": "Core-Router-01",
    "managementIP": "10.0.1.1",
    "isActive": true,
    "uptime": 1250.50,
    "protocols": ["BGP", "OSPF"],
    "location": {
      "datacenter": "DC-Primary",
      "rack": "A-12"
    },
    "notes": null
  }
}

Invalid JSON

JSON
{
  networkDevice: {
    deviceId: 12345,
    hostname: 'Core-Router-01',
    managementIP: "10.0.1.1",
    isActive: True,
    uptime: 1250.50,
    protocols: ["BGP", "OSPF",],
    location: {
      datacenter: "DC-Primary",
      rack: "A-12"
    },
    notes: undefined
  }
}

Unquoted keys (networkDevice, deviceId, hostname, etc.)

Single quotes around 'Core-Router-01'

Capitalized True instead of true

Trailing comma in protocols array

undefined instead of null

Tools for Working with JSON Syntax

Related Tools & Resources

Summary

Understanding JSON syntax is essential for working with modern web APIs and data exchange. Remember the key rules:

  • Always use double quotes for strings and property names
  • No trailing commas after the last item
  • Boolean and null must be lowercase
  • Comments are not allowed in JSON
  • Use our validator to check your syntax

Next Steps: Practice with our JSON examples or test your JSON using our JSON Formatter.