JSON Guide: Complete Tutorial on Syntax, Data Types, Parsing, and Validation

Published: October 5, 2025Updated: February 26, 202616 min read

JSON (JavaScript Object Notation) has become the universal language of data exchange on the web. Created by Douglas Crockford in the early 2000s as a lightweight alternative to XML, JSON is now the default format for REST APIs, configuration files, NoSQL databases (MongoDB, CouchDB, DynamoDB), message queues, and inter-service communication in microservice architectures. Its simplicity — just objects, arrays, and a handful of primitive types — makes it easy to learn, but mastering JSON means understanding its subtleties around parsing, validation, security, and performance.

This guide goes beyond basic syntax to cover everything a developer needs: data types, nested structures, parsing in multiple languages, JSON Schema validation, security considerations, and performance optimization for large datasets.

1. What Is JSON?

JSON is a text-based data format that represents structured data using two universal structures: key-value pairs (objects) and ordered lists (arrays). It was originally derived from JavaScript object literal syntax, but JSON is language-independent — parsers and generators exist for virtually every programming language.

JSON became standardized as ECMA-404 and is also described in RFC 8259. Its success comes from several design decisions:

  • Minimal syntax: Only six structural characters: {} [] : ,
  • Human-readable: Easy to read and write without special tools
  • Native types: Strings, numbers, booleans, null, objects, and arrays cover most data needs
  • Direct language mapping: JSON structures map directly to dictionaries/maps and arrays/lists in most languages
  • No comments: By design, JSON doesn't support comments, which keeps parsing simple and prevents configuration drift

2. JSON Syntax: Objects and Arrays

Objects

An object is an unordered collection of key-value pairs enclosed in curly braces. Keys must be strings (in double quotes). Values can be any JSON type. Keys should be unique within an object (duplicates are technically valid but behavior varies by parser).

{
  "firstName": "Maria",
  "lastName": "Garcia",
  "age": 34,
  "isActive": true,
  "address": null
}

Arrays

An array is an ordered collection of values enclosed in square brackets. Values don't need to be the same type (heterogeneous arrays are valid JSON), though homogeneous arrays are more common and easier to work with.

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

[1, "two", true, null, {"nested": "object"}]

[
  {"id": 1, "name": "Widget"},
  {"id": 2, "name": "Gadget"},
  {"id": 3, "name": "Doohickey"}
]

3. Data Types in Depth

JSON supports exactly six data types. Understanding their nuances prevents subtle bugs:

String

Unicode text enclosed in double quotes. Supports escape sequences: \" (quote),\\ (backslash), \n (newline),\t (tab), \uXXXX (Unicode). Single quotes are NOT valid in JSON (unlike JavaScript).

"Hello, world!"    "Line 1\nLine 2"    "Price: \u20AC10"

Number

Integer or floating-point. JSON numbers must be finite — NaN,Infinity, and -Infinity are NOT valid JSON. No leading zeros (except 0 itself). Supports scientific notation.

42    3.14159    -17    2.998e8    0

Boolean

Exactly true or false (lowercase).True, TRUE,1, and 0 are NOT valid JSON booleans.

Null

The value null (lowercase) represents the intentional absence of value. Different from an empty string "", zero, or undefined (which doesn't exist in JSON).

4. Nested and Complex Structures

JSON's power comes from nesting objects and arrays to represent complex, hierarchical data. There's no depth limit, though readability and performance degrade with excessive nesting.

{
  "company": "Acme Corp",
  "departments": [
    {
      "name": "Engineering",
      "headcount": 45,
      "teams": [
        {
          "name": "Backend",
          "lead": "Jordan",
          "members": ["Alex", "Sam", "Pat", "Robin"],
          "technologies": ["Go", "PostgreSQL", "Redis"]
        },
        {
          "name": "Frontend",
          "lead": "Taylor",
          "members": ["Casey", "Morgan"],
          "technologies": ["React", "TypeScript", "Next.js"]
        }
      ]
    },
    {
      "name": "Design",
      "headcount": 12,
      "teams": [
        {
          "name": "Product Design",
          "lead": "Dakota",
          "members": ["Avery", "Quinn"],
          "technologies": ["Figma", "Framer"]
        }
      ]
    }
  ],
  "founded": 2018,
  "public": false
}

Design tip: Keep nesting to 3-4 levels when possible. If your JSON goes deeper, consider whether the structure can be flattened or referenced by ID rather than embedded.

5. Parsing JSON in JavaScript, Python, and Java

JavaScript

// Parse JSON string to object
const data = JSON.parse('{"name": "Alice", "age": 30}');
console.log(data.name); // "Alice"

// Convert object to JSON string
const json = JSON.stringify(data, null, 2); // pretty-printed

// With a reviver function (for type coercion)
const parsed = JSON.parse(jsonString, (key, value) => {
  if (key === 'date') return new Date(value);
  return value;
});

// NEVER use eval() to parse JSON — security risk!
// eval('(' + jsonString + ')'); // DON'T DO THIS

Python

import json

# Parse JSON string
data = json.loads('{"name": "Alice", "age": 30}')
print(data["name"])  # "Alice"

# Read JSON from file
with open("data.json", "r") as f:
    data = json.load(f)

# Write JSON to file (pretty-printed)
with open("output.json", "w") as f:
    json.dump(data, f, indent=2, ensure_ascii=False)

# Custom serialization for non-standard types
import datetime
class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.isoformat()
        return super().default(obj)

Java

// Using Jackson (most popular)
import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();

// Parse JSON to object
User user = mapper.readValue(jsonString, User.class);

// Parse JSON to map (when class isn't available)
Map<String, Object> map = mapper.readValue(jsonString,
    new TypeReference<Map<String, Object>>() {});

// Convert object to JSON
String json = mapper.writerWithDefaultPrettyPrinter()
    .writeValueAsString(user);

6. JSON Schema: Validating Structure and Types

JSON Schema is a vocabulary for annotating and validating JSON documents. It defines what properties are required, what types they must be, value constraints, and structural rules. JSON Schema is essential for API contracts, configuration validation, and form generation.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User Profile",
  "type": "object",
  "properties": {
    "username": {
      "type": "string",
      "minLength": 3,
      "maxLength": 30,
      "pattern": "^[a-zA-Z0-9_]+$"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "integer",
      "minimum": 13,
      "maximum": 150
    },
    "roles": {
      "type": "array",
      "items": { "type": "string" },
      "minItems": 1,
      "uniqueItems": true
    }
  },
  "required": ["username", "email"],
  "additionalProperties": false
}

JSON Schema supports powerful features beyond basic type checking:

  • Conditional validation: if/then/else for context-dependent rules
  • Composition: allOf, anyOf, oneOf for combining schemas
  • References: $ref for reusing schema definitions across documents
  • Format validation: Built-in formats for email, URI, date-time, IPv4, IPv6, and more

7. Common Syntax Errors and How to Fix Them

Trailing comma after last item

JSON strictly forbids trailing commas. {"a": 1, "b": 2,} is invalid. Remove the comma after the last property or array element.

Single quotes instead of double quotes

JSON requires double quotes for strings and keys. {'name': 'Alice'} is invalid. JavaScript allows single quotes; JSON does not.

Unquoted keys

Every key must be a quoted string. {name: "Alice"} is valid JavaScript but invalid JSON.

Comments in JSON

JSON does not support comments (// or /* */). If you need commented config files, consider JSONC (JSON with Comments), JSON5, or YAML instead.

NaN, Infinity, or undefined values

These are JavaScript concepts that don't exist in JSON. Use null for missing values, and strings or special number representations for non-finite values.

8. JSON Security: Pitfalls to Avoid

JSON parsing is generally safe, but there are important security considerations:

  • Never use eval() to parse JSON. It executes arbitrary code and is a critical vulnerability. Always use JSON.parse() or equivalent library functions.
  • Validate untrusted input. User-submitted JSON should be validated against a schema before processing. Malformed or excessively nested JSON can be used for denial-of-service attacks.
  • Watch for prototype pollution (JavaScript). Parsing JSON with keys like "__proto__" or "constructor" can modify prototype chains. Use Object.create(null) or sanitize keys.
  • Limit payload size. Accept a maximum Content-Length for JSON requests to prevent memory exhaustion. A few megabytes is reasonable for API payloads.
  • Beware of number precision. JSON numbers are typically parsed as IEEE 754 doubles, which can't precisely represent integers larger than 2^53. For large IDs (like Twitter snowflake IDs), use strings.

9. Performance: Working with Large JSON Files

Standard JSON parsers load the entire document into memory. For small payloads (under 10 MB), this is fast and convenient. For larger datasets, consider these strategies:

  • Streaming parsers: Process JSON token-by-token without loading the whole document. In Node.js, use libraries like stream-json. In Python, use ijson. In Java, use the Jackson Streaming API.
  • JSON Lines (JSONL): For datasets with many records, use one JSON object per line. This allows line-by-line processing and easy parallelization. Widely used in log processing and data science.
  • Compression: gzip or brotli compress JSON effectively (often 80-90% reduction) because of repeated keys. Most HTTP clients and servers support transparent compression.
  • Binary alternatives: For extreme performance, consider MessagePack, CBOR, Protocol Buffers, or Avro. These are 2-10x smaller and faster than JSON but sacrifice human readability.

10. JSON in Databases

Modern databases have strong JSON support, blurring the line between relational and document models:

MongoDB

Stores documents as BSON (binary JSON). Supports querying nested fields, array operations, and aggregation pipelines. The most popular document database.

PostgreSQL

Offers json and jsonb column types with full indexing, path queries, and operators. You get relational joins AND document flexibility in one database.

DynamoDB

AWS's serverless NoSQL database stores items as JSON-like documents with native support for maps, lists, and sets. Designed for millisecond latency at any scale.

11. JSON-LD and Structured Data for SEO

JSON-LD (JSON for Linking Data) is a method of encoding structured data using JSON. Search engines like Google use JSON-LD to understand page content and display rich results (recipes, products, events, FAQs) in search listings.

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Article",
  "headline": "JSON Guide: Complete Tutorial",
  "author": {
    "@type": "Organization",
    "name": "JDApplications"
  },
  "datePublished": "2025-10-05",
  "dateModified": "2026-02-26",
  "description": "Comprehensive JSON guide for developers."
}
</script>

JSON-LD is Google's recommended format for structured data. It's embedded in a <script> tag and doesn't affect visual content, making it easy to add to any page without changing the HTML layout.

12. Best Practices for Production JSON

  • Use consistent naming: Pick camelCase or snake_case for keys and apply it everywhere. Mixed conventions confuse consumers.
  • Always use double quotes: The JSON spec requires double quotes. No exceptions.
  • Keep numbers as numbers: Don't stringify numeric values unless you need to preserve formatting (like leading zeros in postal codes).
  • Use null for missing values: Prefer null over empty strings or omitting the key, so consumers can distinguish "no value" from "empty value" from "field doesn't exist."
  • Use ISO 8601 for dates: "2026-02-26T14:30:00Z" is unambiguous and parseable in every language.
  • Validate with JSON Schema: Define schemas for your APIs and validate requests/responses. This catches integration bugs early.
  • Don't embed sensitive data: Tokens, passwords, and API keys should never appear in JSON responses sent to clients.
  • Version your API responses: Include a version field or use URL versioning so changes don't break existing consumers.
  • Paginate large collections: Return arrays with pagination metadata (total count, page number, next URL) instead of dumping thousands of items.

Next Steps

Ready to work with JSON and XML together? Use ourJSON to XML converter to transform your data instantly in the browser, or explore ourXML basics guide to understand the other side of the conversion.

For a detailed comparison of when to use each format, check out ourXML vs JSON analysis.