Getting started with DeltaJSON - NPM Package
You can also use the power of DeltaJSON directly in your code using our NPM package. DeltaJSON is the most powerful JSON comparison tool in NPM with configuration options and JSON result file combining all data from both documents with changes inline for easy processing. Download from NPM here:
deltajson - npm (npmjs.com)
Installation
Sign up here to get a subscription if you haven’t already:
DeltaJSON NPM Sign-up - DeltaXML
Login and get your token file and place this in the same folder as the root directory of your project. To install the deltajson package, navigate to your desired location in your terminal, and type the following commands.
npm install deltajson
npm link
In your development environment, place the token file in the root directory of your project. Make sure you add the filename to your .gitignore file.
echo 'DeltaJSON_Auth.json' >> .gitignore
Getting started in Node
Import the package in your .js file
import deltajson from 'deltajson'
Once that has been completed, the function deltajson.compare() will be available for you to use. Here is an example of how to use the compare function:
let Result = await deltajson.compare('Path/to/fileA.json','Path/to/fileB.json')
Your JSON will be compared and the result can then be further processed as required. Note: the compare function is calling a rest service, you will need to use the "await/async" functions to be able to receive the results.
Now you are ready to configure your comparison using additional parameters which are described below in the Usage section, or in further detail on our DeltaJSON documentation.
Getting started on the command Line
Once you have signed up, download and place the token file in the same folder as the package.
Navigate in the terminal to the same directory that the package is in and run this command. This will run a preset sample to give you an example of how the comparison works.
npm run sample
The above sample will run a basic comparison. This is an example of the sample running the below code:
deltajson compare samples/fileA.json samples/fileB.json result.json
You can replace fileA and fileB with your own files and give it a try.
deltajson compare [Path/to/your/first/JSON] [Path/to/your/second/JSON] [Result Name].json
Configuration
Configuration parameters for each operation
Parameters | Options | Default |
---|---|---|
wordByWord | true/false | false |
arrayAlignment | Orderless/Type With Value/Position | Orderless |
dxConfig | ignoreChanges | null |
Usage
Default JSON Comparison
When using the package in node the following lines will do the default comparison and return a json.
import deltajson from 'deltajson'
let Result = await deltajson.compare('Path/to/fileA.json','Path/to/fileB.json')
When using the command line, the following fields are all required and should be typed in in the following order. This will do a comparison with the default parameters set.
npmjson
compare
Path the first json file
Path to second json file
Result file name
deltajson compare fileA.json fileB.json result.json
wordByWord
The parameter wordByWord will give you a more granualar result. This is set to false by default.
let result = await deltajson.compare('fileA.json','fileB.json',{"wordByWord":"true"});
deltajson compare fileA.json fileB.json result.json wordByWord=true
Click here for more information on how wordByWord works.
arrayAlignment
This parameter gives you control over how items in arrays are matched for comparison. The options for arrayAlignment are as follows:
orderless (default)
let result = await deltajson.compare('fileA.json','fileB.json',{"arrayAlignment":"Orderless"});
deltajson compare fileA.json fileB.json result.json arrayAlignment=orderless
typeWithValuePriority
let result = await deltajson.compare('fileA.json','fileB.json',{"arrayAlignment":"Orderless"});
deltajson compare fileA.json fileB.json result.json arrayAlignment=typeWithValuePriority
positionPriority
let result = await deltajson.compare('fileA.json','fileB.json',{"arrayAlignment":"positionPriority"});
deltajson compare fileA.json fileB.json result.json arrayAlignment=positionPriority
Find out more about how arrayAlignment works in our DeltaJSON documentation.
dxConfig
dxConfig is used for additional transformations of the json file, for instructions on how to use this setting, go to our website
let result = await deltajson.compare('fileA.json','fileB.json',{"dxConfig":"[path/to/dxconfig.json]" or
"dxConfig":"{ \"dxConfig\" : [{ \"ignoreChanges\" : \"delete\",\"key\" : \"hobbies\" }]}"});
deltajson compare fileA.json fileB.json result.json dxConfig=""