Installations
npm install @ziflow/json-diff-ts
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.18.1
NPM Version
8.19.2
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (98.52%)
JavaScript (1.48%)
Developer
ltwlf
Download Statistics
Total Downloads
275
Last Day
2
Last Week
3
Last Month
7
Last Year
49
GitHub Statistics
111 Stars
299 Commits
27 Forks
4 Watching
10 Branches
12 Contributors
Bundle Size
7.41 kB
Minified
2.63 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.2.6
Package Id
@ziflow/json-diff-ts@1.2.6
Unpacked Size
33.26 kB
Size
8.48 kB
File Count
9
NPM Version
8.19.2
Node Version
16.18.1
Publised On
17 Feb 2023
Total Downloads
Cumulative downloads
Total Downloads
275
Last day
100%
2
Compared to previous day
Last week
200%
3
Compared to previous week
Last month
600%
7
Compared to previous month
Last year
-78.3%
49
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
json-diff-ts
json-diff-ts
is a TypeScript library that calculates and applies differences between JSON objects. A standout feature is its ability to identify elements in arrays using keys instead of indices, which offers a more intuitive way to handle arrays. It also supports JSONPath, a query language for JSON, which enables you to target specific parts of a JSON document with precision.
Another significant feature of this library is its ability to transform changesets into atomic changes. This means that each change in the data can be isolated and applied independently, providing a granular level of control over the data manipulation process.
This library is particularly valuable for applications where tracking changes in JSON data is crucial. It simplifies the process of comparing JSON objects and applying changes. The support for key-based array identification can be especially useful in complex JSON structures where tracking by index is not efficient or intuitive. JSONPath support further enhances its capabilities by allowing precise targeting of specific parts in a JSON document, making it a versatile tool for handling JSON data.
Starting with version 3, json-diff-ts
now supports both ECMAScript Modules and CommonJS. This makes the library more versatile and compatible with various JavaScript environments. Whether you're working in a modern project that uses ES modules, or a Node.js project that uses CommonJS, you can now use json-diff-ts
seamlessly.
Installation
1npm install json-diff-ts
In TypeScript or ES Modules, you can import the diff
function like this:
1import { diff } from 'json-diff-ts';
In CommonJS, you can import the diff function like this:
1const { diff } = require('json-diff-ts');
Capabilities
diff
Generates a difference set for JSON objects. When comparing arrays, if a specific key is provided, differences are determined by matching elements via this key rather than array indices.
Examples using Star Wars data:
1import { diff } from 'json-diff-ts'; 2 3const oldData = { 4 planet: 'Tatooine', 5 faction: 'Jedi', 6 characters: [ 7 { id: 'LUK', name: 'Luke Skywalker', force: true }, 8 { id: 'LEI', name: 'Leia Organa', force: true } 9 ], 10 weapons: ['Lightsaber', 'Blaster'] 11}; 12 13const newData = { 14 planet: 'Alderaan', 15 faction: 'Rebel Alliance', 16 characters: [ 17 { id: 'LUK', name: 'Luke Skywalker', force: true, rank: 'Commander' }, 18 { id: 'HAN', name: 'Han Solo', force: false } 19 ], 20 weapons: ['Lightsaber', 'Blaster', 'Bowcaster'] 21}; 22 23const diffs = diff(oldData, newData, { embeddedObjKeys: { characters: 'id' } }); 24 25const expectedDiffs = [ 26 { 27 type: 'UPDATE', 28 key: 'planet', 29 value: 'Alderaan', 30 oldValue: 'Tatooine' 31 }, 32 { 33 type: 'UPDATE', 34 key: 'faction', 35 value: 'Rebel Alliance', 36 oldValue: 'Jedi' 37 }, 38 { 39 type: 'UPDATE', 40 key: 'characters', 41 embeddedKey: 'id', 42 changes: [ 43 { 44 type: 'UPDATE', 45 key: 'LUK', 46 changes: [ 47 { 48 type: 'ADD', 49 key: 'rank', 50 value: 'Commander' 51 } 52 ] 53 }, 54 { 55 type: 'ADD', 56 key: 'HAN', 57 value: { 58 id: 'HAN', 59 name: 'Han Solo', 60 force: false 61 } 62 }, 63 { 64 type: 'REMOVE', 65 key: 'LEI', 66 value: { 67 id: 'LEI', 68 name: 'Leia Organa', 69 force: true 70 } 71 } 72 ] 73 }, 74 { 75 type: 'UPDATE', 76 key: 'weapons', 77 embeddedKey: '$index', 78 changes: [ 79 { 80 type: 'ADD', 81 key: '2', 82 value: 'Bowcaster' 83 } 84 ] 85 } 86];
Advanced
Paths can be utilized to identify keys within nested arrays.
1const diffs = diff(oldData, newData, { embeddedObjKeys { 'characters.subarray': 'id' }});
You can also designate the root by using '.' instead of an empty string ('').
1const diffs = diff(oldData, newData, { embeddedObjKeys: { '.characters.subarray': 'id' } });
Determine if type changes are treated as a replace (remove, add) or as an update; default is replace.
1const diffs = diff(oldData, newData, { treatTypeChangeAsReplace: false });
You can use a function to dynamically resolve the key of the object. The first parameter is the object and the second is to signal if the function should return the key name instead of the value. This is needed to flatten the changeset
1const diffs = diff(oldData, newData, { 2 embeddedObjKeys: { 3 characters: (obj, shouldReturnKeyName) => (shouldReturnKeyName ? 'id' : obj.id) 4 } 5});
If you're using the Map type, you can employ regular expressions for path identification.
1const embeddedObjKeys: EmbeddedObjKeysMapType = new Map(); 2 3embeddedObjKeys.set(/^char\w+$/, 'id'); // instead of 'id' you can specify a function 4 5const diffs = diff(oldObj, newObj, { embeddedObjKeys });
Compare string arrays by value instead of index
1const diffs = diff(oldObj, newObj, { embeddedObjKeys: { stringArr: '$value' } });
atomizeChangeset
Transforms a complex changeset into a list of atomic changes, each describable by a JSONPath.
Examples:
1const atomicChanges = atomizeChangeset(diffs); 2// Restore the changeset from a selection of atomic changes 3const changeset = unatomizeChangeset(flatChanges.slice(0, 3)); 4// Alternatively, apply the changes using a JSONPath-capable library 5// ...
Atomic Changes will have the following structure:
1[ 2 { type: 'UPDATE', key: 'planet', value: 'Alderaan', oldValue: 'Tatooine', path: '$.planet', valueType: 'String' }, 3 // ... Additional flat changes here 4 { type: 'ADD', key: 'rank', value: 'Commander', path: "$.characters[?(@.id=='LUK')].rank", valueType: 'String' } 5];
applyChange
Examples:
1const oldData = { 2 // ... Initial data here 3}; 4 5// Sample diffs array, similar to the one generated in the diff example 6const diffs = [ 7 // ... Diff objects here 8]; 9 10changesets.applyChanges(oldData, diffs); 11 12expect(oldData).to.eql({ 13 // ... Updated data here 14});
revertChange
Examples:
1const newData = { 2 // ... Updated data here 3}; 4 5// Sample diffs array 6const diffs = [ 7 // ... Diff objects here 8]; 9 10changesets.revertChanges(newData, diffs); 11 12expect(newData).to.eql({ 13 // ... Original data restored here 14});
jsonPath
The json-diff-ts
library uses JSONPath to address specific parts of a JSON document in both the changeset and the application/reversion of changes.
Examples:
1 2const jsonPath = changesets.jsonPath; 3 4cost data = { 5 // ... Some JSON data 6}; 7 8const value = jsonPath.query(data, '$.characters[?(@.id=="LUK")].name'); 9 10expect(value).to.eql(['Luke Skywalker']);
Contributing
Contributions are welcome! Please follow the provided issue templates and code of conduct.
Contact
Reach out to the maintainer via LinkedIn or Twitter:
- LinkedIn: Christian Glessner
- Twitter: @leitwolf_io
Discover more about the company behind this project: hololux
Release Notes
- v4.0.0: Change naming of flattenChangest and unflattenChanges to atomizeChangeset and unatomizeChangeset; option to set treatTypeChangeAsReplace
- v3.0.1: Fix issue with unflattenChanges when a key has periods
- v3.0.0: Supports CommonJS and ECMAScript Modules. Dependency to lodash-es was replaced with lodash to support both ECMAScript and CommonJS.
- v2.2.0: Fix lodash-es decependency, exclude keys, compare string arrays by value
- v2.1.0: Resolves a problem related to JSON Path filters by replacing the single equal sign (=) with a double equal sign (==). This update maintains compatibility with existing flat changes. Allows to use either '' or '.' as root in the path.
- v2.0.0: json-diff-ts has been upgraded to an ECMAScript module! This major update brings optimizations and enhanced documentation. Additionally, a previously existing issue where all paths were treated as regex has been fixed. In this new version, you'll need to use a Map instead of a Record for regex paths. Please note that this is a breaking change if you were using regex paths in the previous versions.
- v1.2.6: Enhanced JSON Path handling for period-inclusive segments.
- v1.2.5: Patched dependencies; added key name resolution support for key functions.
- v1.2.4: Documentation updates; upgraded TypeScript and Lodash.
- v1.2.3: Dependency updates; switched to TypeScript 4.5.2.
- v1.2.2: Implemented object key resolution functions support.
Acknowledgments
This project takes inspiration and code from diff-json by viruschidai@gmail.com.
License
json-diff-ts is open-sourced software licensed under the MIT license.
The original diff-json project is also under the MIT License. For more information, refer to its license details.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 28 commits out of 30 are checked with a SAST tool
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
Reason
Found 2/5 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:45: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:56: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:70: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/npm.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/npm.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/npm.yml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/npm.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/npm.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/ltwlf/json-diff-ts/npm.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/npm.yml:34
- Info: 0 out of 10 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 2 out of 3 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:28
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:29
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/npm.yml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Score
4.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More