What Is JSON Diff
JSON Diff compares two JSON data structures and identifies differences. It detects additions, deletions, and modifications between two JSON objects.
Comparison Example
Original JSON:
{
"name": "Alice",
"age": 28,
"hobbies": ["reading", "gaming"]
}
Modified JSON:
{
"name": "Alice",
"age": 29,
"hobbies": ["reading", "coding"],
"email": "alice@example.com"
}
Diff Result:
{
"age": { "old": 28, "new": 29 },
"hobbies[1]": { "old": "gaming", "new": "coding" },
"email": { "added": "alice@example.com" }
}
Why You Need JSON Diff
- API response validation: Compare API returns with expected values
- Config change detection: Compare configuration file modifications
- Data migration verification: Confirm data migration correctness
- Code review: Compare JSON output between two versions
- Automated testing: Assert data changes in tests
- Version control: Track JSON data change history
Diff Result Types
| Change Type | Description | Marker |
|-------------|-------------|--------|
| Added | Key doesn't exist in original | + or added |
| Deleted | Key exists in original but not modified | - or deleted |
| Changed | Key exists but values differ | ~ or changed |
| Unchanged | Key and value are the same | No marker |
How to Use an Online Tool
Using DevToolkit Pro's JSON Diff Tool:
- Enter original JSON on the left
- Enter modified JSON on the right
- The tool auto-compares and highlights differences
- Shows specific additions, deletions, and changes
- Copy Diff results supported
JSON Diff in Code
JavaScript
// Deep compare two objects
function deepDiff(oldObj, newObj, path = '') {
const diffs = {};
const allKeys = new Set([...Object.keys(oldObj), ...Object.keys(newObj)]);
for (const key of allKeys) {
const currentPath = path ? `${path}.${key}` : key;
if (!(key in oldObj)) {
diffs[currentPath] = { type: 'added', value: newObj[key] };
} else if (!(key in newObj)) {
diffs[currentPath] = { type: 'deleted', value: oldObj[key] };
} else if (typeof oldObj[key] === 'object' && typeof newObj[key] === 'object') {
Object.assign(diffs, deepDiff(oldObj[key], newObj[key], currentPath));
} else if (oldObj[key] !== newObj[key]) {
diffs[currentPath] = { type: 'changed', old: oldObj[key], new: newObj[key] };
}
}
return diffs;
}
Python
def deep_diff(old, new, path=''):
diffs = {}
all_keys = set(list(old.keys()) + list(new.keys()))
for key in all_keys:
current_path = f"{path}.{key}" if path else key
if key not in old:
diffs[current_path] = {'type': 'added', 'value': new[key]}
elif key not in new:
diffs[current_path] = {'type': 'deleted', 'value': old[key]}
elif isinstance(old[key], dict) and isinstance(new[key], dict):
diffs.update(deep_diff(old[key], new[key], current_path))
elif old[key] != new[key]:
diffs[current_path] = {'type': 'changed', 'old': old[key], 'new': new[key]}
return diffs
FAQ
Can JSON Diff Handle Large Files?
Depends on implementation. Simple in-memory comparison may be slow for large files (>10MB). Use streaming Diff algorithms or specialized libraries (deep-diff, jsondiffpatch).
Can Diff Results Be Merged?
Some tools support "merge" functionality, applying Diff results to the original JSON. This requires the Diff to contain complete change info (old and new values).
How to Handle Array Order Changes?
Array Diff has two strategies: position-based comparison (simple but may be inaccurate) and content-based comparison (smarter but more complex). Choose based on your use case.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
Complete Guide to YAML JSON Conversion: Principles, Tools, and Best Practices
Deep dive into the differences and connections between YAML and JSON. Master bidirectional YAML JSON conversion methods — online tools, CLI, code implementations, common pitfalls, and best practices.
Online XML/JSON Converter: Convert Between Data Formats
Learn how to convert between XML and JSON formats. Understand the differences between XML and JSON, and use cases in API integration, configuration management, and data exchange.
Online Markdown Preview: Real-time Render and Edit Markdown
Learn how to use an online Markdown previewer to render Markdown documents in real time. Understand Markdown syntax, GFM extensions, tables, code highlighting, and best practices for technical documentation.