JSON Syntax: A Complete Guide for Developers


JSON (JavaScript Object Notation) is the most widely used data format for APIs and configuration files. Understanding its syntax is essential for every developer.


What is JSON?


JSON is a lightweight, text-based data interchange format. Despite its name, JSON is language-independent and used across virtually every programming language.


Basic Structure


JSON is built on two structures:


Objects


Objects are collections of key-value pairs enclosed in curly braces:


{"name": "John", "age": 30}


Keys must be strings in double quotes. Values can be any valid JSON data type.


Arrays


Arrays are ordered lists of values enclosed in square brackets:


["apple", "banana", "cherry"]


Arrays can contain any combination of data types, including other arrays and objects.


Data Types


JSON supports six data types:


Strings


Strings must be enclosed in double quotes (not single quotes):


{"message": "Hello, World!"}


Special characters must be escaped with a backslash.


Numbers


Numbers can be integers or floating-point, positive or negative:


{"integer": 42, "float": 3.14, "negative": -10}


JSON doesn't support special values like NaN or Infinity.


Booleans


Boolean values are lowercase true or false:


{"isActive": true, "isDeleted": false}


Null


Null represents an empty or non-existent value:


{"middleName": null}


Objects


Objects can be nested within other objects:


{"person": {"name": "John", "age": 30}}


Arrays


Arrays can contain any data type:


{"numbers": [1, 2, 3], "mixed": [1, "two", true, null]}


Common Syntax Rules


Keys Must Be Strings


Keys must always be double-quoted strings:


Correct: {"name": "John"}

Wrong: {name: "John"}


No Trailing Commas


JSON doesn't allow trailing commas:


Correct: {"a": 1, "b": 2}

Wrong: {"a": 1, "b": 2,}


No Comments


JSON doesn't support comments. This is one of its limitations:


Wrong: {"name": "John" // this is a comment}


Double Quotes Only


Always use double quotes, never single quotes:


Correct: {"name": "John"}

Wrong: {'name': 'John'}


Escaping Special Characters


Use backslash to escape special characters in strings:


  • " - Double quote
  • \\ - Backslash
  • \n - Newline
  • \t - Tab
  • \r - Carriage return

  • Example: {"message": "Line 1\nLine 2"}


    Nested Structures


    JSON excels at representing complex, nested data:


    {

    "company": "Acme Corp",

    "employees": [

    {"name": "John", "role": "Developer"},

    {"name": "Jane", "role": "Designer"}

    ],

    "address": {

    "street": "123 Main St",

    "city": "New York"

    }

    }


    Whitespace


    JSON ignores whitespace between elements. These are equivalent:


    {"name":"John"}


    {

    "name": "John"

    }


    Use whitespace for readability in development, remove it for production to reduce file size.


    Conclusion


    JSON's simple syntax makes it easy to read and write. Use JSONSpark to format, validate, and minify your JSON data quickly.

    Need to format or validate JSON?

    Try JSONSpark for instant formatting, validation, and minification.

    Try JSONSpark Free