JSON Parser Node
The JSON Parser node converts JSON strings to JavaScript objects and handles JSON validation, formatting, and transformation operations.
Overview
This node provides robust JSON parsing capabilities with error handling, validation, and support for various JSON operations including parsing, stringifying, and data extraction.
Configuration
Required Parameters
Parameter | Type | Description |
---|---|---|
input | String/Object | JSON string to parse or object to stringify |
operation | String | Operation type: parse, stringify, validate, extract |
Optional Parameters
Parameter | Type | Default | Description |
---|---|---|---|
path | String | null | JSONPath expression for data extraction |
strict | Boolean | true | Enable strict JSON parsing |
pretty | Boolean | false | Pretty-print output (for stringify) |
indent | Number | 2 | Indentation spaces (for pretty print) |
removeComments | Boolean | false | Remove JSON comments before parsing |
Input Schema
{
"input": "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}",
"operation": "parse",
"strict": true,
"path": "$.name"
}
Output Schema
{
"success": true,
"result": {
"name": "John",
"age": 30,
"city": "New York"
},
"type": "object",
"size": 45,
"extracted": "John"
}
Operations
Parse JSON String
Converts JSON string to JavaScript object.
// Input
{
"input": "{\"users\": [{\"id\": 1, \"name\": \"Alice\"}, {\"id\": 2, \"name\": \"Bob\"}]}",
"operation": "parse"
}
// Output
{
"success": true,
"result": {
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
}
}
Stringify Object
Converts JavaScript object to JSON string.
// Input
{
"input": {"message": "Hello World", "timestamp": 1642434567},
"operation": "stringify",
"pretty": true
}
// Output
{
"success": true,
"result": "{\n \"message\": \"Hello World\",\n \"timestamp\": 1642434567\n}"
}
Validate JSON
Checks if input is valid JSON without parsing.
// Input
{
"input": "{\"valid\": true, \"test\": 123}",
"operation": "validate"
}
// Output
{
"success": true,
"valid": true,
"errors": []
}
Extract Data (JSONPath)
Extracts specific data using JSONPath expressions.
// Input
{
"input": {
"store": {
"book": [
{"title": "Book 1", "price": 10.99},
{"title": "Book 2", "price": 15.99}
]
}
},
"operation": "extract",
"path": "$.store.book[*].title"
}
// Output
{
"success": true,
"extracted": ["Book 1", "Book 2"]
}
JSONPath Examples
Error Handling
Error Handling
Parse Errors
{
"success": false,
"error": "SyntaxError: Unexpected token } in JSON at position 15",
"line": 1,
"column": 16,
"input": "{\"name\": \"John\",}"
}
Path Errors
{
"success": false,
"error": "Invalid JSONPath expression: $.invalid[",
"path": "$.invalid["
}
Advanced Features
Comment Removal
Handle JSON with comments (non-standard):
// Input
{
"input": "{\n // User data\n \"name\": \"John\",\n /* age */ \"age\": 30\n}",
"operation": "parse",
"removeComments": true
}
Custom Revivers
Transform values during parsing:
// Input
{
"input": "{\"date\": \"2024-01-15T10:30:00Z\"}",
"operation": "parse",
"reviver": "function(key, value) { return key === 'date' ? new Date(value) : value; }"
}
Large JSON Handling
For large JSON files, use streaming mode:
// Input
{
"input": "large-file.json",
"operation": "parse",
"streaming": true,
"chunkSize": 1024
}
Schema Validation
Validate JSON against a schema:
// Input
{
"input": {"name": "John", "age": 30},
"operation": "validate",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "number", "minimum": 0}
},
"required": ["name", "age"]
}
}
// Output
{
"success": true,
"valid": true,
"schemaValid": true
}
Best Practices
- Error Handling: Always handle parse errors gracefully
- Memory Usage: Be careful with large JSON objects
- Validation: Validate JSON structure before processing
- Security: Sanitize JSON input to prevent injection attacks
- Performance: Use streaming for large files
- Type Safety: Validate expected data types after parsing
Performance Considerations
- Use streaming for files > 10MB
- Avoid deep nesting (> 100 levels)
- Cache parsed results when possible
- Use specific JSONPath expressions over recursive searches
- Consider using binary formats for large datasets
Related Nodes
- XML Parser - Parse XML data
- CSV Parser - Parse CSV files
- YAML Parser - Parse YAML format
- Data Validator - Validate data structures
Examples & Use Cases
API Response Processing
// Parse API response and extract user emails
{
"input": "{\"users\": [{\"email\": \"user1@example.com\"}, {\"email\": \"user2@example.com\"}]}",
"operation": "extract",
"path": "$.users[*].email"
}
Configuration File Processing
// Parse and validate configuration
{
"input": "{\"database\": {\"host\": \"localhost\", \"port\": 5432}}",
"operation": "parse",
"validate": true,
"schema": {...}
}
Data Transformation
// Transform nested JSON structure
{
"input": {"data": {"items": [1, 2, 3]}},
"operation": "extract",
"path": "$.data.items[*]",
"transform": "flatten"
}
Troubleshooting
Common Issues
-
Invalid JSON Syntax
- Use JSON validators to check syntax
- Enable pretty printing for readability
-
Large File Memory Issues
- Enable streaming mode
- Process in chunks
-
JSONPath Not Working
- Verify path syntax
- Test with simpler expressions first
-
Circular References
- Handle circular references in stringify
- Use custom replacer functions