Complete Tree Comparison Module
Use the Complete Tree Comparison Module to compare files with tree-structured data like JSON or XML files. Tricentis Data Integrity has no limitations on file size, so you can easily compare large trees.
The Complete Tree Comparison Module is part of the Tosca Data Integrity Modules And Samples.tsu subset.
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 treats every value as a string for the comparison.
-
XML to XML. These comparisons verify structural alignment and check for value and attribute mismatches.
This table explains important differences between JSON and XML files for comparisons:
|
JSON |
XML |
---|---|---|
Types |
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 reports them as different in type. |
XML files only allow string types. |
Attributes |
It's not possible to define attributes in JSON. |
XML treats most data as text, but attributes and CDATA sections can have specific text. |
Data Structure |
Both JSON and XML organize data hierarchically. Tosca reports missing target and source nodes. |
|
Value |
If a node has the same path and type in source and target, for example “1” and 1, Tosca checks whether the node's value matches and reports the result. |
Module Attributes
The Complete Tree Comparison Module has the following attributes:
ModuleAttribute |
Description |
---|---|
Source |
Specify the absolute path to the source file. |
Target |
Specify the absolute path to the target file. |
General Options |
Specify the report file destination, as well as included and excluded paths. |
General Options
In General Options you can define the following:
ModuleAttribute |
Description |
Optional |
---|---|---|
Report Path |
Local file path to the folder where the comparison report is saved. |
X |
Tree nodes to ignore during testing. |
X |
|
Tree nodes to include during testing. Tosca ignores all other paths. |
X |
Create tests with the Module
To compare two JSON or XML files, perform the following steps:
-
Create a TestCase 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.
-
-
Choose if you want to use the following under General Options:
-
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 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.
-
Examples
Let's use the following files for a test.
Source.json is our source 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 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.
Tree comparison test case with filter
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:
-
Matches the root element person.
-
Compares the child nodes name, age, and contacts.
Additionally, for this JSON to XML comparison, Tosca:
-
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.