JSON1

Complete JSON Guide

Deep dive into JSON (JavaScript Object Notation): the standard format for modern data exchange

What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format. Despite having “JavaScript” in its name, JSON is actually a language-independent data format, with native support for parsing and generation in virtually all modern programming languages.

Core Features

Human Readable

Clear text format with intuitive key-value pair structure

Machine Friendly

Simple syntax rules, fast parsing speed

Language Independent

Supported by all major programming languages

Lightweight

More concise than XML, smaller transmission size

JSON Syntax Rules

JSON is built on two structures:

  • Objects: A collection of name/value pairs (similar to dictionaries, hash tables)
  • Arrays: An ordered list of values (similar to arrays or vectors)

Basic Syntax Example

{
  "name": "John Doe",
  "age": 30,
  "isActive": true,
  "address": {
    "city": "New York",
    "zipCode": "10001"
  },
  "hobbies": ["programming", "reading", "traveling"],
  "spouse": null
}

Data Types Explained

1. String

Zero or more Unicode characters enclosed in double quotes

"Hello World", "你好", "123", ""

2. Number

Decimal numbers, supporting integers and floating-point

42, -17, 3.14159, 0, 1.0e+2

3. Boolean

Logical value with only two possible values

true, false

4. null

Represents null value or absence of value

null

5. Object

Unordered collection of key/value pairs

{ "key": "value", "number": 123 }

6. Array

Ordered collection of values

[1, 2, 3], ["a", "b", "c"], [true, false, null]

Use Cases

Web APIs

Standard data format for modern Web APIs, used for data exchange between client and server.

{
  "status": "success",
  "data": {
    "users": [
      {
        "id": 1,
        "username": "admin"
      }
    ]
  }
}

Configuration Files

Many applications use JSON as configuration file format.

{
  "database": {
    "host": "localhost",
    "port": 3306
  },
  "logging": {
    "level": "info"
  }
}

Data Storage

Widely used for data storage, caching, and network transmission. NoSQL databases like MongoDB natively support JSON format.

AJAX Communication

In web development, JSON is the preferred data format for AJAX requests, used for asynchronous data exchange.

Best Practices

âś… Recommended Practices

  • • Use meaningful key names: "firstName" instead of "fn"
  • • Maintain consistent naming conventions: camelCase or snake_case
  • • Use appropriate data types: numbers for numeric values, booleans for boolean values
  • • Avoid deep nesting, keep structure flat
  • • Use arrays to represent list data

❌ Practices to Avoid

  • • Using single quotes: JSON must use double quotes
  • • Adding comments: Standard JSON doesn't support comments
  • • Using undefined: JSON doesn't support undefined values
  • • Unquoted key names: All key names must be enclosed in double quotes
  • • Trailing commas: JSON doesn't allow trailing commas

Common Errors

Syntax Errors

❌ Incorrect Examples

{
  'name': 'John',  // Single quotes
  age: 30,         // Unquoted key
  "city": "NYC",   // Trailing comma
}

âś… Correct Examples

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

Character Encoding Issues

JSON must use UTF-8 encoding. Ensure special characters are properly escaped, such as backslashes should be written as \\\\.

JSON Tools Recommendations

JSON Tools Available on This Site

Formatting

Beautify JSON structure for better readability

Validation

Check JSON syntax correctness

Compression

Remove whitespace to reduce file size

Conversion

Convert between JSON, XML, and CSV formats

Escaping

Handle escape character strings

Visualization

Tree structure display

Read this in another language