Complete Tree Comparison module
Use the Complete Tree Comparison Module to run reconciliation tests that compare files with tree-structured data like JSON or XML files. Tosca Cloud has no limitations on file size, so you can compare large trees.
Understand what you can compare
The module helps you to detect differences in hierarchical or nested data. You can detect and report missing nodes and differences in types. You can compare the following:
-
JSON to JSON. These comparisons verify structural alignment and check for value and type mismatches, for example string vs. number.
-
JSON to XML. These comparisons consider both formats' hierarchical structures, which are mapped to a tree. Attributes don’t exist in JSON and Tosca Cloud treats every value as a string for the comparison.
-
XML to XML. These comparisons verify structural alignment and check for value and attribute mismatches.
Both JSON and XML organize data hierarchically in nested nodes. Tosca Cloud reports missing target and source nodes. If a node has the same path and type in source and target, for example “1” and 1, Tosca Cloud checks whether the node's value matches and reports the result.
This table explains how Tosca Cloud treats differences between JSON and XML files during tests:
|
File type |
Data Types |
Attributes |
|---|---|---|
|
JSON |
JSON files may contain multiple data types, such as strings, numbers, booleans, arrays, or objects. With JSON to JSON comparisons, the structure and all data types must match. If they don't, Tosca Cloud reports them as different in type. |
It's not possible to define attributes in JSON. |
|
XML |
XML files only allow string types |
XML treats most data as text, but attributes and CDATA sections can have specific text. XML to XML comparisons focus on structure and textual content. Tosca Cloud reports differences in attributes. |
Module Attributes
The Complete Tree Comparison Module has the following attributes:
|
Value |
Description |
Optional |
|---|---|---|
|
Source |
The absolute path to the source file. |
|
|
Target |
The absolute path to the target file. |
|
|
General Options->Report Path |
Local file path to the folder where the comparison report is saved. You can use our Report Viewer to display this report in an accessible format. |
X |
|
General Options->Paths to Exclude |
Tree nodes to ignore during testing. |
X |
|
General Options->Paths to Include |
Tree nodes to include during testing. Tosca Cloud ignores all other paths. |
X |
|
Result |
A successful comparison returns the result Files match*. Enter this as the value and use the ActionMode Verify. This verifies the test against the result. If this comparison fails, so does your test. |
|
Create tests with the module
To compare two JSON or XML files, perform the following steps:
-
Create a test case and add the Complete Tree Comparison module to it.
-
Define your Source and Target files:
-
Select the correct type for each file.
-
Set their absolute paths under each Value.
-
-
Source and Target have the same functionality, so you can use them interchangeably.
-
The Report Path specifies the folder where your tree comparison report gets saved. Fill in this value with a local file path. After you run your test, you can open the saved report with our Report Viewer. If you leave this value empty, Tosca Cloud won't save a report.
-
The Paths to Exclude specifies the list of all the paths and nodes you wish to exclude from the tree comparison.
-
The Paths to Include specifies the list of all the paths and nodes you wish to compare. All other paths are excluded.
Paths to Include and Paths to Exclude apply both to Target and Source. Using both filters in the same test applies the Paths to Include first, and then excludes what is defined in the exclusion filter. The module compares the full tree file if you leave both these values empty.
The nodepath syntax is the full node path with support for wildcards, as in the examples below. Note that there should be no spaces in the path.
-
Examples
Let's use the following files for a test.
Source.json is our file, located at C:\Examples\Tree Comparison\Sources\source.json
{
"library": {
"books": [
{
"title": "The Hobbit",
"price": 14.99,
"genre": "Fantasy"
},
{
"title": "Pride and Prejudice",
"price": 10.25,
"genre": "Romance"
}
],
"magazines": [
{
"title": "National Geographic",
"price": 5.99,
"genre": "Science"
},
{
"title": "Time",
"price": 4.50,
"genre": "News"
},
"END"
]
}
}
Target.json is our target file, located at C:\Examples\Tree Comparison\Targets\target.json:
{
"library": {
"books": [
{
"title": "The Hobbit 2",
"price": 14.99,
"genre": "Fantasy"
},
{
"title": "Pride and Prejudice",
"price": 10.99,
"genre": "Romance"
}
],
"magazines": [
{
"title": "National Geographic",
"price": 5.99,
"genre": "Science"
},
{
"title": "Time",
"price": 4.50,
"genre": "News"
},
]
}
}
Tosca Cloud compares the files in the following way:
-
It matches the root element library.
-
It compares the child nodes books, and magazines.
-
It reports the node book[0] as mismatched due to the node title: The Hobbit <> The Hobbit 2.
-
It reports the node book[1] as mismatched due to the node price: 10.25 <> 10.99.
-
It reports additional nodes like END as missing in the target file.
-
It reports the rest of the nodes as matching.
Exclude or include paths
Here are some examples on how to use Paths to Include and Paths to Exclude when comparing the above files:
-
To only compare books set Paths to Include as library.books[*].
-
To only compare prices set Paths to Include as library.books[*].price;library.magazines[*].price.
-
To only compare specific values set Paths to Include as library.books[0].title;library.magazines[1].price.
-
To only compare book prices set Paths to Include as library.books[*].price.
-
To exclude the first magazine from the comparison set Paths to Exclude as library.magazines[0].
-
To exclude all titles and prices from the comparison set Pats to Exclude as library.books[*].price;library.magazines[*].price;library.books[*].title;library.magazines[*].title or Paths to Include as library.books[*].genre; library.magazines[*].genre.
If you use the last filter example and remove the attribute END from Source.json, both files become equal in structure and value.
JSON to XML comparison
You may also compare this Source1.json file:
{
"person": {
"name": "Alice",
"age": 30,
"contacts": [
{"contact": "alice@example.com"},
{"contact": "123-456-7890"},
]
}
}
To Target1.xml:
<person>
<name>Alice</name>
<age>30</age>
<contacts>
<contact>alice@example.com</contact>
<contact type="phone">123-456-7890</contact>
</contacts>
</person>
Similar to the previous example, Tosca Cloud:
-
Matches the root element person.
-
Compares the child nodes name, age, and contacts.
Additionally, for this JSON to XML comparison, Tosca Cloud:
-
Maps the contacts array in the JSON file to the contacts element in the XML file. The individual objects in the JSON array map to the contact elements in the XML file.
-
It reports attributes such as phone as missing in the JSON file.
If you remove the attribute type="phone" from the Target1.xml file, both files are equal in structure and value.