FTJ
← Blog
Developer

JSON Validation: Ensuring Data Integrity

Learn how to validate JSON data, understand common errors, use JSON Schema, and ensure data integrity in your applications.

# JSON Validation: Ensuring Data Integrity

JSON validation is critical for ensuring data integrity in modern applications. This guide covers common errors, validation techniques, and tools to keep your JSON data clean and valid.

Common JSON Errors

1. Missing or Extra Commas

Wrong: `json { "name": "John", "age": 30, "city": "New York", } `

Correct: `json { "name": "John", "age": 30, "city": "New York" } `

2. Trailing Commas in Arrays

Wrong: `json ["apple", "banana", "orange",] `

Correct: `json ["apple", "banana", "orange"] `

3. Single Quotes Instead of Double Quotes

Wrong: `json {'name': 'John'} `

Correct: `json {"name": "John"} `

4. Unescaped Special Characters

Wrong: `json { "message": "He said "Hello"" } `

Correct: `json { "message": "He said \"Hello\"" } `

5. Comments (Not Valid in JSON)

Wrong: `json { // This is a comment "name": "John" } `

Correct: Use JSON5 or remove comments

Validation Techniques

1. Manual Validation

Use online validators or IDE plugins to check syntax.

2. Programmatic Validation

JavaScript: `javascript function isValidJSON(str) { try { JSON.parse(str); return true; } catch (e) { return false; } } `

Python: `python import json

def is_valid_json(string): try: json.loads(string) return True except json.JSONDecodeError: return False `

3. JSON Schema Validation

JSON Schema is a powerful way to validate the structure and content of JSON data.

Example Schema: `json { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "name": { "type": "string", "minLength": 1 }, "age": { "type": "integer", "minimum": 0 }, "email": { "type": "string", "format": "email" } }, "required": ["name", "age"] } `

JSON Schema Basics

Types

  • string: Text data
  • number: Numeric values (no distinction between int/float)
  • integer: Whole numbers
  • boolean: true/false
  • array: Ordered list
  • object: Key-value pairs
  • null: Empty value

Constraints

  • minimum/maximum: Numeric ranges
  • minLength/maxLength: String length
  • pattern: Regex patterns
  • enum: Allowed values
  • required: Mandatory fields

Advanced Features

  • conditional validation: If-then-else logic
  • references: Reuse schema definitions
  • format: Predefined formats (email, uri, date-time)

Linting Tools

Command Line Tools

jsonlint (Node.js): `bash npm install -g jsonlint jsonlint data.json `

jq (Linux/Mac): `bash jq . data.json `

IDE Integrations

  • VS Code: Built-in JSON validation
  • IntelliJ: JSON Schema mapping
  • Sublime Text: Package Control plugins

Online Validators

  • JSONLint.com
  • JSONSchema.net
  • FreeFormatter.com

Best Practices

  1. Validate Early: Check JSON at input boundaries
  2. Use Schema: Define strict schemas for APIs
  3. Automated Testing: Include JSON validation in test suites
  4. Error Messages: Provide clear, actionable error messages
  5. Version Your Schema: Track changes to validation rules

Common Validation Patterns

API Request Validation

app.post('/api/users', (req, res) => {
  const validator = require('jsonschema');
  const schema = require('./schemas/user.json');
  
  const result = validator.validate(req.body, schema);
  
  if (!result.valid) {
    return res.status(400).json({
      errors: result.errors
    });
  }
  
  // Process valid data
});

Configuration File Validation

Validate configuration files at application startup to fail fast.

Tools to Help

Use our JSON Formatter to format and validate JSON, JSON to CSV Converter to convert validated data, and Diff Checker to compare JSON structures.

Conclusion

JSON validation is not optional - it's essential for robust applications. Combine syntax validation with schema validation for maximum data integrity. Invest in good tooling and automate validation wherever possible.

Try These Tools

More Articles