Skip to main content
The JSON Format/Validate tool provides comprehensive JSON processing capabilities including validation, pretty-printing, minification, and conversion to JavaScript object literals. It includes a permissive parser that accepts loose JSON syntax common in JavaScript.

Features

  • Strict and loose JSON parsing
  • Pretty-print with 2-space indentation
  • Minification for compact output
  • JavaScript object literal generation
  • Detailed error messages with parse hints
  • Automatic trailing comma removal
  • Single-quote to double-quote conversion

Use Cases

API Response Formatting

Format minified JSON from API responses for readability

Configuration Files

Validate and format JSON configuration files before deployment

Code Minification

Reduce JSON payload size for network transmission

JavaScript Migration

Convert loose JavaScript objects to valid JSON format

Actions

Default (Format)

Pretty-prints JSON with 2-space indentation:
{
  "name": "example",
  "value": 123
}

Minify

Removes all whitespace for compact output:
{"name":"example","value":123}

JavaScript Object

Converts to JavaScript object literal syntax:
const data = {
  "name": "example",
  "value": 123
};

Input Formats

Strict JSON

Standard JSON with double-quoted keys:
{"name": "value", "count": 42}

Loose JSON

JavaScript-style syntax with unquoted keys and single quotes:
{name: 'value', count: 42, active: true}
The loose JSON parser automatically normalizes JavaScript object syntax to valid JSON.

Examples

Pretty-print minified JSON.Input:
{"user":{"name":"Alice","age":30,"email":"alice@example.com"},"active":true}
Output:
{
  "user": {
    "name": "Alice",
    "age": 30,
    "email": "alice@example.com"
  },
  "active": true
}

Loose JSON Parser

The tool includes a permissive parser that handles common JavaScript syntax:

Supported Transformations

Input SyntaxNormalization
{name: "value"}Unquoted keys → Quoted keys
{'name': 'value'}Single quotes → Double quotes
{a: 1, b: 2,}Trailing commas → Removed
{a:1}No spaces → Formatted

Implementation

From lib/tools/engine.ts:36-59:
function parseJsonLoose(raw: string) {
  const text = raw.trim();
  if (!text) return {};
  try {
    return JSON.parse(text);
  } catch {
    let s = text;
    // 1. Quote unquoted identifier keys (e.g. {a: 1} → {"a": 1})
    s = s.replace(/([{,]\s*)([A-Za-z_$][\w$-]*)(\s*:(?!\/))/g, '$1"$2"$3');
    // 2. Normalize single-quoted strings to double-quoted
    s = s.replace(/'((?:[^'\\]|\\.)*)'/g, (_, inner) =>
      '"' + inner.replace(/"/g, '\\"').replace(/\\'/g, "'") + '"'
    );
    // 3. Remove trailing commas
    s = s.replace(/,(\s*[}\]])/g, '$1');
    return JSON.parse(s);
  }
}

Error Handling

The tool provides clear error messages:
JSON parse error: Unexpected token at position 23
Common issues:
  • Unquoted keys: Use {"key": "value"} not {key: value} in strict mode
  • Single quotes: Use double quotes for strings in strict mode
  • Trailing commas: Remove commas before closing braces/brackets
  • Comments: JSON does not support // or /* */ comments
The loose parser is designed for convenience but may accept some invalid JavaScript. Always validate critical JSON with a strict parser before deployment.

Common Patterns

Validate API Response

// Copy API response, paste into tool, check for errors
fetch('/api/data')
  .then(r => r.text())
  .then(text => console.log(text)); // Paste this

Format Configuration

// Before: minified config
{"database":{"host":"localhost","port":5432}}

// After: formatted for readability
{
  "database": {
    "host": "localhost",
    "port": 5432
  }
}

Convert JS to JSON

// JavaScript object
const config = {name: 'app', version: '1.0.0'};

// Valid JSON
{"name": "app", "version": "1.0.0"}