Last modified: March 23, 2026

This article is written in: 🇺🇸

JSON

JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. Despite originating from JavaScript, the format is completely language independent and supported by virtually every modern programming language. Its simplicity and direct mapping to common data structures like hash maps and arrays have made it the dominant format for APIs, configuration files, and data exchange on the web.

+----- JSON Document ----------------------------------------+
|                                                            |
|  {                                                         |
|    "key": "string value",     <-- String                   |
|    "count": 42,               <-- Number                   |
|    "active": true,            <-- Boolean                  |
|    "tags": ["a", "b"],        <-- Array                    |
|    "nested": { ... },         <-- Object                   |
|    "empty": null              <-- Null                     |
|  }                                                         |
|                                                            |
+------------------------------------------------------------+

Data Types

JSON defines six primitive value types that combine to represent nearly any data structure:

Syntax Rules

Example

Here is a JSON document showing nested objects, arrays, and multiple data types working together:

{
  "employee": {
    "name": "John Doe",
    "age": 30,
    "active": true,
    "department": "Engineering",
    "address": {
      "street": "1234 Main St",
      "city": "Anytown",
      "state": "CA",
      "zipCode": "12345"
    },
    "skills": ["Java", "C#", "Python"],
    "certifications": null
  }
}

Parsing Flow

When a backend service receives a JSON payload, the text goes through a well-defined parsing pipeline before the application can use the data:

Raw bytes        Decode to       Tokenize         Build AST /       Application
 from network --> UTF-8 text -->  (lexer) -------> data structure --> logic
                                   |                    |
                           Split into tokens:     Map to native
                           { , } , [ , ] ,        types (dict,
                           "key", : , value        list, int, str)

Benefits of JSON

Common Uses

JSON Schema

JSON Schema is a vocabulary that lets you annotate and validate the structure of JSON documents. It acts like a contract between producers and consumers of an API.

JSON Document                    JSON Schema
 +------------------+             +---------------------------+
 | {                |   validate  | {                         |
 |   "age": 30     | ----------> |   "type": "object",       |
 | }                |   against   |   "properties": {         |
 +------------------+             |     "age": {              |
        |                         |       "type": "integer",  |
        v                         |       "minimum": 0        |
   Valid / Invalid                |     }                     |
                                  |   },                      |
                                  |   "required": ["age"]     |
                                  | }                         |
                                  +---------------------------+

JSON Streaming (NDJSON / JSON Lines)

Standard JSON requires loading the entire document into memory before parsing, which is problematic for large datasets. Newline-Delimited JSON (NDJSON), also called JSON Lines, solves this by placing one valid JSON object per line:

{"event": "click", "ts": 1700000001}
{"event": "view",  "ts": 1700000002}
{"event": "click", "ts": 1700000003}

Common Pitfalls

Number Precision

Encoding Issues

Structural Mistakes

Performance Considerations

JSON vs XML

Both JSON and XML are text-based data interchange formats, but they serve different niches. JSON dominates in web APIs and microservices, while XML remains common in enterprise systems, document markup, and contexts that require schemas with namespaces.

Feature JSON (JavaScript Object Notation) XML (eXtensible Markup Language)
Format Lightweight and text-based. Verbose and document-based format.
Readability Generally easier to read due to its concise format. Less human-readable due to more verbose nature.
Data Types Supports string, number, array, boolean, object, and null. Does not have built-in data types; everything is a string.
Parsing Native support in browsers; fast parsers in every language. Requires dedicated XML parsers (DOM, SAX, StAX).
Size Typically smaller, making it faster to transmit. Larger file sizes due to additional markup and closing tags.
Schema Validation JSON Schema (draft standard, widely adopted). XSD and DTD (mature, W3C standards).
Extensibility Limited; fixed structure of objects and arrays. Highly extensible with custom tags, namespaces, and attributes.
Metadata Limited support; metadata mixed into the data structure. Excellent support via attributes and processing instructions.
Comments Does not support comments natively. Supports comments with <!-- --> syntax.
Security Less attack surface; no entity expansion vulnerabilities. Prone to XXE (XML External Entity) and billion-laughs attacks.
Array Support First-class arrays with square brackets []. No built-in array; relies on repeated child elements.
Namespace Support No namespace mechanism. Full namespace support for avoiding naming collisions.
Use Cases Web APIs, microservices, configuration files, mobile apps. Enterprise integrations (SOAP), document markup, RSS/Atom feeds.

Best Practices