Xml To Json
Converts XML data to JSON format for easier processing and manipulation.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
Inputs
- Input XML - The XML text or file path to convert to JSON.
Options
-
Input Format - Specifies the input format:
- Text (default) - Input is XML text content
- File Path - Input is a path to an XML file
-
Output Format - Specifies the output format:
- Text (default) - Output is a JSON object
- File Path - Output is a path to a temporary JSON file
Outputs
- Output JSON - The converted JSON object or file path, depending on the Output Format option.
How It Works
The Xml To Json node converts XML data into JSON format. When executed, the node:
- Receives XML data through the Input XML input (either as text or file path)
- If input is a file path, reads the XML content from the file
- Parses the XML structure into a map
- Converts the XML structure to a JSON object
- If output format is "File Path", writes the JSON to a temporary file
- Outputs either the JSON object or the file path through the Output JSON output
This conversion makes XML data easier to work with in modern automation workflows.
Example Usage
Example 1: Converting Simple XML
Convert basic XML to JSON:
<!-- Input XML text: -->
<person>
<name>John Doe</name>
<age>30</age>
<email>john@example.com</email>
</person>
// Output JSON object:
{
"person": {
"name": "John Doe",
"age": "30",
"email": "john@example.com"
}
}
Example 2: Converting XML with Attributes
XML attributes are preserved in the conversion:
<!-- Input XML text: -->
<product id="123" category="electronics">
<name>Laptop</name>
<price currency="USD">999.99</price>
</product>
// Output JSON object:
{
"product": {
"-id": "123",
"-category": "electronics",
"name": "Laptop",
"price": {
"-currency": "USD",
"#text": "999.99"
}
}
}
Note: Attributes are prefixed with - and text content is stored as #text.
Example 3: Converting XML Arrays
XML elements with the same name become arrays:
<!-- Input XML text: -->
<catalog>
<item>Product A</item>
<item>Product B</item>
<item>Product C</item>
</catalog>
// Output JSON object:
{
"catalog": {
"item": [
"Product A",
"Product B",
"Product C"
]
}
}
Example 4: Converting Complex Nested XML
Handle deeply nested structures:
<!-- Input XML text: -->
<company>
<name>Tech Corp</name>
<departments>
<department>
<name>Engineering</name>
<employees>
<employee>
<name>Alice</name>
<role>Developer</role>
</employee>
<employee>
<name>Bob</name>
<role>Manager</role>
</employee>
</employees>
</department>
</departments>
</company>
// Output JSON object:
{
"company": {
"name": "Tech Corp",
"departments": {
"department": {
"name": "Engineering",
"employees": {
"employee": [
{
"name": "Alice",
"role": "Developer"
},
{
"name": "Bob",
"role": "Manager"
}
]
}
}
}
}
}
Example 5: Converting XML from File
Process XML file and save result to file:
// Input Format: File Path
// Output Format: File Path
// Input: "/path/to/data.xml"
// Output: "/tmp/xml_to_json_1234567890.json"
Requirements
- Valid XML text or file path as input
- If input format is "File Path", the file must exist
- XML must be well-formed
Error Handling
The node will return specific errors in the following cases:
- ErrInvalidArg - When the input XML is empty
- ErrInvalidArg - When input file does not exist (File Path mode)
- ErrInvalidArg - When failed to read input file
- ErrInvalidArg - When failed to parse XML (malformed XML)
- ErrInvalidArg - When failed to create or write temporary file (File Path output mode)
Usage Tips
- API Integration: Convert XML API responses to JSON for easier processing
- Legacy Systems: Transform XML data from legacy systems to modern JSON format
- Data Migration: Convert XML configuration or data files to JSON
- File Processing: Use File Path mode for large XML files to avoid memory issues
- Object Manipulation: Use JSON package nodes to work with converted data
- Chaining: Combine with Json Encode to get a JSON string representation
Common Use Cases
- SOAP API Integration: Convert SOAP XML responses to JSON for processing
- RSS/Atom Feeds: Parse XML feeds and convert to JSON for data extraction
- Configuration Migration: Convert XML config files to JSON format
- Data Transformation: Transform XML exports from databases or applications
- Web Scraping: Convert XML sitemaps or data to JSON
- Legacy Integration: Bridge XML-based systems with JSON-based workflows
XML to JSON Conversion Rules
- Elements: XML elements become JSON object properties
- Attributes: XML attributes are prefixed with
- - Text Content: When an element has both text and attributes, text is stored as
#text - Repeated Elements: Multiple elements with the same name become a JSON array
- Namespaces: Namespace prefixes are preserved in property names
- CDATA: CDATA sections are treated as text content
Best Practices
- Validation: Validate XML structure before conversion
- Large Files: Use File Path mode for files larger than 10MB
- Error Handling: Enable "Continue On Error" when processing untrusted XML
- Schema Awareness: Understand your XML schema to predict JSON structure
- Memory Management: Monitor memory usage with very large XML files
- Testing: Test conversion with sample data to verify output structure
Common Errors and Solutions
Invalid XML Format
Problem: Error message "Failed to parse XML"
Solutions:
- Verify the XML is well-formed (proper opening/closing tags)
- Check for missing or extra brackets
<> - Ensure special characters are properly escaped (
<,>,&, etc.) - Validate XML using an XML validator tool
- Check for invalid characters in element names
File Not Found
Problem: Error message "Input file does not exist"
Solutions:
- Verify the file path is correct and absolute
- Check file permissions
- Ensure the file hasn't been moved or deleted
- Use the correct path separator for your operating system
Empty Input
Problem: Error message "Input XML cannot be empty"
Solutions:
- Verify the input variable contains data
- Check if the previous node properly set the output
- If reading from file, ensure the file is not empty
- Add conditional logic to handle empty values
Parse Errors
Problem: XML parsing fails with syntax errors
Solutions:
- Ensure all XML tags are properly closed
- Verify XML declaration is correct (
<?xml version="1.0"?>) - Check for unescaped special characters in text content
- Remove or properly encode control characters
- Validate against XML specification
Memory Issues
Problem: Out of memory with large XML files
Solutions:
- Use Input Format "File Path" instead of loading entire content
- Use Output Format "File Path" to write results to disk
- Process large files in chunks if possible
- Increase available memory for the automation
Understanding the Output Structure
Simple Elements
<name>John</name>
{"name": "John"}
Elements with Attributes
<person age="30">John</person>
{
"person": {
"-age": "30",
"#text": "John"
}
}
Empty Elements
<empty/>
{"empty": ""}
Elements with Only Attributes
<person id="123"/>
{
"person": {
"-id": "123"
}
}
Repeated Elements
<items>
<item>A</item>
<item>B</item>
</items>
{
"items": {
"item": ["A", "B"]
}
}
Example Workflow
Processing an XML API response:
// Step 1: Receive XML response from HTTP request
const xmlResponse = `
<response>
<status>success</status>
<data>
<user id="123">
<name>John Doe</name>
<email>john@example.com</email>
</user>
</data>
</response>
`;
// Step 2: Convert XML to JSON using Xml To Json node
// Input Format: Text
// Output Format: Text
// Step 3: Output JSON object
const jsonOutput = {
"response": {
"status": "success",
"data": {
"user": {
"-id": "123",
"name": "John Doe",
"email": "john@example.com"
}
}
}
};
// Step 4: Access data using JSON Get node
// Path: response.data.user.name
// Result: "John Doe"
Working with Namespaces
XML with namespaces:
<root xmlns:custom="http://example.com/custom">
<custom:element>Value</custom:element>
</root>
Converted JSON:
{
"root": {
"-xmlns:custom": "http://example.com/custom",
"custom:element": "Value"
}
}
Notes
- The conversion preserves the XML structure but changes the format
- Attribute names are prefixed with
-to distinguish them from elements - Text content is stored as
#textwhen mixed with attributes - The output JSON can be further processed using JSON package nodes
- Large XML files should use File Path mode for better performance
- The conversion is one-way (this package doesn't include JSON to XML conversion)
- Namespace prefixes are preserved in the JSON property names
- The node uses standard XML parsing libraries ensuring robust conversion
- Temporary files created in File Path mode are stored in the system temp directory