JSONAssertX includes a powerful Visual Diff feature that provides enhanced, developer-friendly error messages when assertions fail. This feature sets JSONAssertX apart from competitors by showing you exactly what went wrong with clear, visual formatting.
Traditional JSON assertion libraries show generic error messages like:
AssertionError: expected "Hello World" but got "Hello Word"
With JSONAssertX's Visual Diff, you get:
═══════════════════════════════════════════════════════
JSON ASSERTION FAILED
═══════════════════════════════════════════════════════
Path: $.message
Expected: "Hello Word"
Actual: "Hello World"
^ First difference at position 9
This 10x faster debugging helps you spot issues immediately without staring at long JSON strings.
Shows exactly where strings differ with a visual marker:
Expected: "Hello Word"
Actual: "Hello World"
^ First difference at position 9
Detects and displays whitespace differences:
Expected: "Hello·World"
Actual: "Hello··World"
^ First difference at position 6
⚠ Strings differ only in whitespace
Clearly shows which elements are extra or missing:
═══════════════════════════════════════════════════════
ARRAY SIZE MISMATCH
═══════════════════════════════════════════════════════
Expected: 2 element(s)
Actual: 4 element(s)
➜ Array has 2 extra element(s)
Actual array content:
[
[0] "apple"
[1] "banana"
[2] ➜ "cherry"
[3] ➜ "date"
]
When an element is not found, shows suggestions for similar values:
═══════════════════════════════════════════════════════
ELEMENT NOT FOUND IN ARRAY
═══════════════════════════════════════════════════════
Looking for: "javascript"
Array contents (3 element(s)):
[0] "java"
[1] "testing"
[2] "json"
💡 Similar values in array:
→ java
Shows which array element failed the predicate test:
═══════════════════════════════════════════════════════
PREDICATE MATCH FAILED
═══════════════════════════════════════════════════════
Predicate type: allMatch
Failed at index: 0 with value: 85
All elements:
[
[0] ✗ 85
[1] 92
[2] ✗ 78
[3] 95
[4] 88
]
Shows the JSON structure and path breakdown:
═══════════════════════════════════════════════════════
PATH NOT FOUND
═══════════════════════════════════════════════════════
Path: $.user.email
Path breakdown:
$.user ✓
$.user.email ✗
JSON structure:
{
"user" : {
"name" : "John",
"age" : 30
}
}
Highlights numeric differences and warns about precision issues:
Expected: 100.00
Actual: 99.99
Difference: -0.01
⚠ Small difference detected (< 0.01). Consider using tolerance-based comparison.
Shows detailed field-by-field differences for complex objects:
═══════════════════════════════════════════════════════
JSON STRUCTURE MISMATCH
═══════════════════════════════════════════════════════
Missing fields: [department]
Extra fields: [role]
Field differences:
age:
Expected: 30
Actual: 25
The Visual Diff feature is implemented in the JsonDiffFormatter utility class with the following methods:
formatDifference(path, expected, actual)- Main entry point for formatting differencesformatStringDiff(expected, actual)- Character-level string comparison with whitespace detectionformatNumberDiff(expected, actual)- Number comparison with precision warningsformatArraySizeMismatch(expectedSize, actualSize, elements)- Array size errors with highlightingformatMissingElement(element, array)- Missing element with similarity suggestionsformatPredicateFailure(type, failedIndex, failedValue, elements)- Predicate failure detailsformatPathNotFound(path, json)- Path not found with structure display
- 10x faster debugging - Spot issues immediately
- Clear visual cues - Symbols and formatting guide your attention
- Context-aware - Shows relevant information based on failure type
- Actionable - Suggests similar values and provides helpful warnings
- AssertJ: Basic diffs, no JSON-specific formatting
- REST Assured: Verbose output, hard to parse
- JSONAssertX: ✓ Visual diffs, ✓ JSON-aware, ✓ Clear formatting
The Visual Diff feature is always enabled - no setup required. All assertion failures automatically use enhanced formatting.
String json = "{\"users\": [\"Alice\", \"Bob\", \"Charlie\"]}";
// This will show visual diff if it fails
JsonAssertX.assertThat(json)
.path("$.users").isArray()
.hasSize(5) // Expected 5, but has 3
.contains("David"); // Will show "David not found, did you mean Bob?"═- Section separators➜- Extra elements in arrays✓- Valid path components✗- Failed elements or invalid paths💡- Helpful suggestions⚠- Warnings·- Visualized spaces
- No performance overhead - Formatting only occurs on assertion failures
- Lazy evaluation - Diffs are computed only when needed
- Memory efficient - Uses streaming for large JSON structures
The JsonDiffFormatter class is designed to be extended:
- Add custom formatters for specific data types
- Customize visual symbols and colors
- Integrate with IDE plugins for syntax highlighting
Planned improvements for the Visual Diff feature:
- Color support - ANSI color codes for terminal output
- Side-by-side diffs - For large JSON objects
- Diff highlighting in IDEs - IntelliJ and VS Code plugins
- Interactive diffs - Web-based viewer for CI/CD
- Custom formatters - Plugin system for domain-specific types
We built Visual Diff based on developer pain points. If you have suggestions for improvements, please:
- Open an issue on GitHub
- Submit a PR with your enhancement
- Share your use case in Discussions
JSONAssertX - JSON Assertions Made Visual ✨