Utility Coder
← Back to Blog
Data Processing10 min read

Data Validation: JSON, CSV, and XML Format Guide

Master data validation for JSON, CSV, and XML formats. Learn syntax rules, common errors, and automated validation techniques.

By Andy Pham

Data Validation: JSON, CSV, and XML Format Guide

Data validation ensures your data files are properly formatted and error-free. This guide covers validation for the most common data formats.

JSON Validation

JSON Syntax Rules

  1. Data is in key/value pairs
  2. Keys must be strings (double-quoted)
  3. Values can be: string, number, object, array, boolean, null
  4. Objects use curly braces {}
  5. Arrays use square brackets []
  6. Items separated by commas (no trailing comma)

Common JSON Errors

// ERROR: Single quotes
{'name': 'John'}  // Wrong
{"name": "John"}  // Correct

// ERROR: Trailing comma
{"a": 1, "b": 2,}  // Wrong
{"a": 1, "b": 2}   // Correct

// ERROR: Unquoted keys
{name: "John"}    // Wrong
{"name": "John"}  // Correct

// ERROR: Single value without wrapper
"just a string"   // Valid JSON
just a string     // Invalid

JSON Validation Code

function validateJSON(str) {
  try {
    JSON.parse(str);
    return { valid: true };
  } catch (e) {
    return {
      valid: false,
      error: e.message,
      position: e.message.match(/position (d+)/)?.[1]
    };
  }
}

// Schema validation with Ajv
const Ajv = require('ajv');
const ajv = new Ajv();

const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number', minimum: 0 }
  },
  required: ['name']
};

const validate = ajv.compile(schema);
const valid = validate({ name: 'John', age: 30 });

CSV Validation

CSV Format Rules

  1. Each record on a separate line
  2. Fields separated by delimiter (usually comma)
  3. Fields with special chars should be quoted
  4. Double quotes inside quoted fields: use two double quotes

Common CSV Issues

# Unbalanced quotes
name,description
John,"Missing end quote

# Inconsistent columns
name,age,city
John,30
Jane,25,NYC,extra

# Unescaped quotes
name,bio
John,"He said "hello""  // Wrong
John,"He said ""hello"""  // Correct

CSV Validation Code

function validateCSV(str, options = {}) {
  const { delimiter = ',', hasHeader = true } = options;
  const lines = str.trim().split('
');
  const errors = [];

  let expectedColumns = null;

  lines.forEach((line, index) => {
    // Parse respecting quotes
    const columns = parseCSVLine(line, delimiter);

    if (index === 0 && hasHeader) {
      expectedColumns = columns.length;
    } else if (columns.length !== expectedColumns) {
      errors.push({
        line: index + 1,
        message: `Expected ${expectedColumns} columns, got ${columns.length}`
      });
    }
  });

  return {
    valid: errors.length === 0,
    errors,
    rowCount: lines.length,
    columnCount: expectedColumns
  };
}

function parseCSVLine(line, delimiter) {
  const result = [];
  let current = '';
  let inQuotes = false;

  for (let i = 0; i < line.length; i++) {
    const char = line[i];

    if (char === '"') {
      if (inQuotes && line[i + 1] === '"') {
        current += '"';
        i++;
      } else {
        inQuotes = !inQuotes;
      }
    } else if (char === delimiter && !inQuotes) {
      result.push(current);
      current = '';
    } else {
      current += char;
    }
  }

  result.push(current);
  return result;
}

XML Validation

XML Syntax Rules

  1. Must have a root element
  2. Tags must be properly nested
  3. Tags are case-sensitive
  4. Attribute values must be quoted
  5. Special characters must be escaped

Common XML Errors

<!-- Missing root element -->
<item>1</item>
<item>2</item>

<!-- Should be -->
<items>
  <item>1</item>
  <item>2</item>
</items>

<!-- Improperly nested -->
<b><i>text</b></i>  <!-- Wrong -->
<b><i>text</i></b>  <!-- Correct -->

<!-- Unescaped special characters -->
<data>5 < 10</data>  <!-- Wrong -->
<data>5 &lt; 10</data>  <!-- Correct -->

<!-- Unquoted attributes -->
<item id=1>  <!-- Wrong -->
<item id="1">  <!-- Correct -->

XML Validation Code

function validateXML(str) {
  const parser = new DOMParser();
  const doc = parser.parseFromString(str, 'application/xml');

  const parseError = doc.querySelector('parsererror');
  if (parseError) {
    return {
      valid: false,
      error: parseError.textContent
    };
  }

  return { valid: true, document: doc };
}

// Node.js with libxmljs
const libxmljs = require('libxmljs');

function validateXMLWithSchema(xml, xsd) {
  const xmlDoc = libxmljs.parseXml(xml);
  const xsdDoc = libxmljs.parseXml(xsd);

  const isValid = xmlDoc.validate(xsdDoc);
  return {
    valid: isValid,
    errors: xmlDoc.validationErrors
  };
}

Validation Best Practices

1. Validate Early

// Validate on input
function handleFileUpload(file) {
  const reader = new FileReader();
  reader.onload = (e) => {
    const result = validateJSON(e.target.result);
    if (!result.valid) {
      showError(result.error);
      return;
    }
    processData(JSON.parse(e.target.result));
  };
  reader.readAsText(file);
}

2. Provide Clear Errors

function formatValidationError(error) {
  return {
    message: error.message,
    line: error.line,
    column: error.column,
    suggestion: getSuggestion(error.code)
  };
}

3. Use Schema Validation

// JSON Schema for type safety
const userSchema = {
  type: 'object',
  properties: {
    email: { type: 'string', format: 'email' },
    age: { type: 'integer', minimum: 0, maximum: 150 }
  },
  required: ['email']
};

Try Our Validators

Conclusion

Proper data validation prevents errors and ensures data integrity. Use appropriate validators for each format and implement schema validation for stricter type checking.

Share this article