Data Processing•10 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
- Data is in key/value pairs
- Keys must be strings (double-quoted)
- Values can be: string, number, object, array, boolean, null
- Objects use curly braces
{} - Arrays use square brackets
[] - 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
- Each record on a separate line
- Fields separated by delimiter (usually comma)
- Fields with special chars should be quoted
- 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
- Must have a root element
- Tags must be properly nested
- Tags are case-sensitive
- Attribute values must be quoted
- 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 < 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
- JSON Validator - Validate JSON syntax
- CSV Validator - Check CSV format
- XML Validator - Validate XML documents
Conclusion
Proper data validation prevents errors and ensures data integrity. Use appropriate validators for each format and implement schema validation for stricter type checking.