Install our app 🪄 click on the icon in the top right of the address bar.

How to Validate JSON: 5 Methods Every Developer Should Know

Tutorials & Guides 4 March, 2026 6 min read 9 views

    Learn 5 ways to validate JSON — online tools, command line, JavaScript, Python, and IDE plugins. Fix JSON syntax errors fast.

    TL;DR

    You can validate JSON using an online tool, the command-line (jq or Python), JavaScript's JSON.parse() in a try/catch, Python's json.loads(), or IDE extensions. Each method suits a different workflow. For quick checks, use an online JSON validator. For automation, use the command line or a language method.

    Key Takeaways
    • All 5 methods are free — no paid tools needed to validate JSON syntax.
    • The most common JSON error is a trailing comma after the last item in an object or array, which is not allowed by RFC 8259.
    • python -m json.tool is the fastest CLI method on any system with Python installed — no extra packages needed.
    • JSON.parse() throws a SyntaxError with the exact character position of the error, making it useful for debugging.
    • IDE extensions like Prettier and ESLint validate JSON on every save without any manual step.

    Why Does JSON Validation Matter?

    Invalid JSON causes silent failures. An API returning malformed JSON will crash the client application with an unhelpful error. A misconfigured package.json with a trailing comma will prevent npm install from running. A broken webhook payload will fail silently and lose data.

    JSON is defined in RFC 8259, which specifies an exact grammar. The rules are strict: no trailing commas, no comments, all strings must use double quotes, and numbers cannot have leading zeros. Any deviation makes the entire document invalid.

    Method 1: Online JSON Validator (Fastest for One-Off Checks)

    An online validator is the fastest way to check JSON when you are not in a coding environment or working in a browser. Paste your JSON and the tool highlights errors with line numbers and error descriptions.

    1. Go to ToolSparkr's JSON Formatter & Validator.
    2. Paste your JSON into the input field.
    3. Click Format / Validate.
    4. If valid, your JSON is formatted with proper indentation. If invalid, an error message with the position of the problem is shown.
    5. Use the Minify button to remove whitespace for production use.

    This method requires no installation and works on any device. It is ideal for validating API responses you have copied from browser DevTools or Postman.

    Method 2: Command Line with Python (No Extra Packages)

    If you have Python installed (Python 3 is available by default on macOS and most Linux distributions), you can validate JSON without installing anything extra:

    echo '{"name":"Alice","active":true}' | python3 -m json.tool

    If valid, Python prints the formatted JSON. If invalid, it prints the error:

    json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 18 (char 17)

    To validate a file:

    python3 -m json.tool myfile.json > /dev/null && echo "Valid" || echo "Invalid"

    The > /dev/null suppresses the formatted output so you only see the pass/fail message.

    Method 3: Command Line with jq (More Powerful)

    jq is a command-line JSON processor available for Linux, macOS, and Windows. It validates JSON and also lets you query and transform it.

    1. Install jq: brew install jq (macOS) or sudo apt install jq (Ubuntu/Debian).
    2. Validate a file: jq . myfile.json > /dev/null
    3. If invalid, jq prints the parse error with the line and column number.
    4. Validate a URL response: curl -s https://api.example.com/data | jq .

    jq is the best command-line tool for validation when you also need to extract specific fields or transform the data.

    Method 4: JavaScript JSON.parse() in a Try/Catch

    JSON.parse() throws a SyntaxError if the input is not valid JSON. Wrap it in a try/catch to handle the error gracefully:

    function validateJSON(text) {
      try {
        const parsed = JSON.parse(text);
        console.log('Valid JSON:', parsed);
        return { valid: true, data: parsed };
      } catch (err) {
        console.error('Invalid JSON:', err.message);
        return { valid: false, error: err.message };
      }
    }
    
    // Example usage
    const result = validateJSON('{"name":"Alice","active":true}');
    // Valid JSON: { name: 'Alice', active: true }
    
    const bad = validateJSON('{"name":"Alice",}');
    // Invalid JSON: Unexpected token } in JSON at position 17
    import json
    
    def validate_json(text):
        try:
            data = json.loads(text)
            print('Valid JSON:', data)
            return {'valid': True, 'data': data}
        except json.JSONDecodeError as e:
            print(f'Invalid JSON: {e.msg} at line {e.lineno}, col {e.colno}')
            return {'valid': False, 'error': str(e)}
    
    # Example usage
    validate_json('{"name": "Alice", "active": true}')
    # Valid JSON: {'name': 'Alice', 'active': True}
    
    validate_json('{"name": "Alice",}')
    # Invalid JSON: Expecting property name enclosed in double quotes at line 1, col 19

    The JavaScript error message includes the character position. The Python JSONDecodeError includes line number, column number, and the specific problem. Both are suitable for use in production code that processes external JSON input.

    Method 5: IDE Extensions (Continuous Validation)

    IDE extensions validate JSON as you type, highlighting errors before you save or run the code. This is the most efficient approach for everyday development work.

    1. VS Code: JSON validation is built-in. Open any .json file and VS Code underlines errors automatically. No extension needed for basic validation.
    2. Prettier: Install the Prettier extension and enable Format on Save. Prettier will refuse to format invalid JSON, making invalid files immediately visible.
    3. ESLint with json plugin: Install eslint-plugin-json to lint JSON files alongside JavaScript. Useful in monorepos.
    4. JetBrains IDEs (IntelliJ, WebStorm, PhpStorm): JSON validation and schema validation are built-in. Right-click any JSON file and select Validate to check against a JSON Schema.
    5. JSON Schema: In VS Code, add a $schema key to your JSON file pointing to a schema URL (e.g., https://json.schemastore.org/package). VS Code will validate all keys and values against the schema, not just syntax.

    What Are the Most Common JSON Syntax Errors?

    Error Invalid Example Fix
    Trailing comma {"a":1,} Remove the comma before }
    Single quotes {'key':'value'} Use double quotes: {"key":"value"}
    Unquoted keys {key: "value"} Quote all keys: {"key":"value"}
    Comments {"a":1 // comment} Remove comments — JSON does not support them
    Undefined value {"a": undefined} Use null instead of undefined
    NaN or Infinity {"a": NaN} Use null or a numeric string

    Validate Your JSON Now

    Use the free JSON Formatter & Validator to paste, validate, and format any JSON in seconds. The tool shows the exact line and column of any error.

    JSON validation checks that the text follows RFC 8259 syntax — valid structure, correct quotes, no trailing commas. JSON Schema validation (using a schema file) goes further and checks that the values match expected types, required fields are present, and values are within defined ranges. You need JSON Schema validation when you care about the content, not just the structure.
    Some JavaScript engines have historically been lenient about certain edge cases, but modern V8 (Node.js, Chrome) and SpiderMonkey (Firefox) both strictly follow RFC 8259. If you see a discrepancy, check that you are using a current runtime version. Online validators also follow the spec strictly.
    Yes. Use python3 -m json.tool filename.json > /dev/null or jq . filename.json > /dev/null in a pipeline step. These commands exit with code 1 if the JSON is invalid, which will fail the build. For JSON Schema validation in CI, use ajv-cli (Node.js) or jsonschema (Python).
    "Unexpected token" means the parser encountered a character it did not expect at that position. Common causes: a trailing comma introduces } before the parser expects it, single quotes appear where double quotes are required, or a JavaScript keyword like undefined appears where a JSON value is expected.
    JSONC is not a standard — it is a VS Code-specific extension used for configuration files like tsconfig.json and settings.json. Standard validators will reject JSONC because of the comments. To validate JSONC programmatically, use the jsonc-parser npm package, which strips comments before parsing.

    Related Posts

    Share this article
    Written by ToolSparkr Team
    Our team of developers and writers creates free, in-depth guides to help you make the most of every online tool. From encoding to hashing, SEO to security — we've got you covered.
    Browse all tools