JSON Diff Kit - A better JSON differ & viewer

Overview

JSON Diff Kit

NPM version Downloads

A better JSON differ & viewer.

Notice: considering most of the browsers now support ES6, this library has only the ES6 version. If you need the compatibility with older browsers, please configure the compiler in your project (e.g. add this library to the include field in babel.config.js).

Install

You can install json-diff-kit via various package managers.

# using npm
npm i json-diff-kit --save

# using yarn
yarn add json-diff-kit

# using pnpm
pnpm add json-diff-kit

Quick Start

To generate the diff data:

import { Differ } from 'json-diff-kit';

const before = {
  a: 1,
  b: 2,
  d: [1, 5, 4],
  e: ['1', 2, { f: 3, g: null, h: [5], i: [] }, 9],
};
const after = {
  b: 2,
  c: 3,
  d: [1, 3, 4, 6],
  e: ['1', 2, 3, { f: 4, g: false, i: [7, 8] }, 10],
};

// all configs are optional
const differ = new Differ({
  detectCircular: true,    // default `true`
  maxDepth: Infinity,      // default `Infinity`
  showModifications: true, // default `true`
  arrayDiffMethod: 'lcs',  // default `"normal"`
});

const diff = differ.diff(before, after);
console.log(diff);

You can use your own component to visualize the diff data, or use the built-in viewer:

import { Viewer } from 'json-diff-kit';
import type { DiffResult } from 'json-diff-kit';

import 'json-diff-kit/dist/viewer.css';

interface PageProps {
  diff: [DiffResult[], DiffResult[]];
}

const Page: React.FC<PageProps> = props => {
  return (
    <Viewer
      diff={props.diff}  // required
      indent={4}         // default `2`
      lineNumbers={true} // default `false`
    />
  );
};

The result is here:

The result (using LCS array diff method).

More Complex Usages

Please check the demo pages.

Algorithm Details

Please refer to the article JSON Diff Kit: A Combination of Several Simple Algorithms.

License

MIT

Comments
  • Add option to hide in the viewer parts that have not changed

    Add option to hide in the viewer parts that have not changed

    I'm trying to use JSONDiffKit to visualize diff between two, relatively big JSON payloads. Usually the diff is only about 5 properties out of the whole structure.

    The current problem I face is out of 480 lines the viewer show only 7 actually have change but I still see the whole structure.

    Of course the solution is not as simple as just hiding any line that haven't been changed because you can have an inner object have a single property change but you still want to see the parent properties to understand the context of the change.

    Do you think it is possible to add the ability to somehow decide which lines to hide to make the diff more concise and easy to understand?

    opened by ido-ran 3
  • Fix import error in certain environment

    Fix import error in certain environment

    Fixes #4, #9

    "type": "module" might also be desirable to indicate that the code is using ESM, and I do have it in my local patch working fine, but not sure if this would interfere with the build process of the package so didn't include it.

    opened by alexturpin 1
  • SyntaxError: Cannot use import statement outside a module

    SyntaxError: Cannot use import statement outside a module

    Hi, there are already a few issues due to build problems however the error I'm getting is not among them.

    After adding an import of the package to my next.js project (using TypeScript) I get the following error:

    error - ...\node_modules\json-diff-kit\dist\index.js:2453
    import * as React from "react";
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
        at Object.compileFunction (node:vm:360:18)
        at wrapSafe (node:internal/modules/cjs/loader:1088:15)
        at Module._compile (node:internal/modules/cjs/loader:1123:27)
        at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
        at Module.load (node:internal/modules/cjs/loader:1037:32)
        at Module._load (node:internal/modules/cjs/loader:878:12)
        at Module.require (node:internal/modules/cjs/loader:1061:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at json-diff-kit (...\.next\server\pages\audit.js:339:18)
        at __webpack_require__ (...\.next\server\webpack-runtime.js:33:42) {
      page: '/audit'
    }
    

    The mentioned (compiled) file does indeed contain the import: grafik

    This is the only import in this compiled file so I guess it is a build error and the other imports got adjusted properly.

    Maybe this import could be adjusted to just import the used function/types/etc and not everything and that might resolve the issue: https://github.com/RexSkz/json-diff-kit/blob/2b60eddfab505b5b46dd4352401c411bae4b1c57/src/viewer.tsx#L1

    Also just in case this import exists because it was necessary to import React in older versions of React, this is no longer the case.

    opened by profiluefter 1
  • Error [ERR_MODULE_NOT_FOUND]: Cannot find package

    Error [ERR_MODULE_NOT_FOUND]: Cannot find package

    install the package with pnpm add json-diff-kit and run the demo image

    demo code: nodejs 19.2 // mytest.mjs

    import { Differ,Viewer } from 'json-diff-kit';
    console.log(Differ,Viewer)
    
    
    opened by rawbin- 1
  • build error

    build error

        ![image](https://user-images.githubusercontent.com/39376011/207543873-719475e2-4b02-4bd0-b55a-4fc5a8247a08.png)
    

    Originally posted by @yhtyanght in https://github.com/RexSkz/json-diff-kit/issues/7#issuecomment-1350617942

    opened by yhtyanght 0
  • Same object detected as newly added

    Same object detected as newly added

    Hello.

    I found out the following issue while running some test cases.

    As you can see I've the same object on the right side and added one object above and one below of it. The library detects that the text of the left object was changed.

    Screenshot 2022-11-29 at 15 31 18
    opened by xhonato 1
  • Getting build errors occurring in my environment with this code

    Getting build errors occurring in my environment with this code

    I pulled in v1.0.8 of this diff tool and then did a build and the build failed with a number of errors (below). I'm looking for how I can get this code to work in my environment which uses Angular 7, typescript 3.2.2 and node v10.17.0. I looked around for what minimum version of typescript and/or node would work but could not find that information.

    npm run build:prod


    ERROR in node_modules/json-diff-kit/typings/differ.d.ts(93,68): error TS1005: ']' expected. node_modules/json-diff-kit/typings/differ.d.ts(93,80): error TS1005: ';' expected. node_modules/json-diff-kit/typings/differ.d.ts(93,82): error TS1005: ';' expected. node_modules/json-diff-kit/typings/index.d.ts(3,1): error TS1128: Declaration or statement expected. node_modules/json-diff-kit/typings/index.d.ts(3,13): error TS1005: ';' expected. node_modules/json-diff-kit/typings/index.d.ts(3,52): error TS1109: Expression expected. node_modules/json-diff-kit/typings/index.d.ts(3,59): error TS1005: ';' expected. node_modules/json-diff-kit/typings/index.d.ts(4,1): error TS1128: Declaration or statement expected. node_modules/json-diff-kit/typings/index.d.ts(4,13): error TS1005: ';' expected. node_modules/json-diff-kit/typings/index.d.ts(4,57): error TS1109: Expression expected. node_modules/json-diff-kit/typings/index.d.ts(4,64): error TS1005: ';' expected. node_modules/json-diff-kit/typings/index.d.ts(5,1): error TS1128: Declaration or statement expected. node_modules/json-diff-kit/typings/index.d.ts(5,13): error TS1005: ';' expected. node_modules/json-diff-kit/typings/index.d.ts(5,28): error TS1109: Expression expected. node_modules/json-diff-kit/typings/index.d.ts(5,35): error TS1005: ';' expected. node_modules/json-diff-kit/typings/viewer.d.ts(2,13): error TS1005: '=' expected. node_modules/json-diff-kit/typings/viewer.d.ts(2,33): error TS1005: ';' expected. node_modules/json-diff-kit/typings/viewer.d.ts(3,13): error TS1005: '=' expected. node_modules/json-diff-kit/typings/viewer.d.ts(3,40): error TS1005: ';' expected. node_modules/json-diff-kit/typings/viewer.d.ts(56,33): error TS1005: ']' expected. node_modules/json-diff-kit/typings/viewer.d.ts(56,35): error TS1131: Property or signature expected. node_modules/json-diff-kit/typings/viewer.d.ts(56,46): error TS1011: An element access expression should take an argument. node_modules/json-diff-kit/typings/viewer.d.ts(56,47): error TS1005: ';' expected. node_modules/json-diff-kit/typings/viewer.d.ts(58,12): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(60,14): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(61,21): error TS1005: ',' expected. node_modules/json-diff-kit/typings/viewer.d.ts(62,24): error TS1005: ',' expected. node_modules/json-diff-kit/typings/viewer.d.ts(63,24): error TS1005: ',' expected. node_modules/json-diff-kit/typings/viewer.d.ts(66,17): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(68,25): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(70,23): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(75,24): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(77,15): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(79,11): error TS1109: Expression expected. node_modules/json-diff-kit/typings/viewer.d.ts(80,1): error TS1128: Declaration or statement expected.

    opened by masordi 1
Owner
Rex Zeng
MUGer, hacker, developer, amateur UI designer, punster, Japanese learner.
Rex Zeng
A tool for writing better scripts

Bash is great, but when it comes to writing scripts, people usually choose a more convenient programming language.

Google 35.5k Dec 27, 2022
A JSON polyfill. No longer maintained.

?? Unmaintained ?? JSON 3 is **deprecated** and **no longer maintained**. Please don't use it in new projects, and migrate existing projects to use th

BestieJS Modules 1k Dec 24, 2022
Generate HTML redirections from json file

Generate HTML redirections from json file

Andrew Luca 3 Jan 6, 2022
🧠 100'den fazla gereksiz bilgi ile oluşturulmuş bir JSON API.

?? Gereksiz Bilgiler API 100'den fazla gereksiz bilgi ile oluşturulmuş bir JSON API.

Orhan Emre Dikicigil 3 Sep 23, 2022
Tooling to automate converting .xlsx localisation to in-game compatible .json files for Vampire Survivors

vampire-survivors-localisation This tooling is used to automate converting .xlsx localisation to in-game compatible .json files for the game Vampire S

null 17 Dec 8, 2022
Defines the communication layer between mobile native(iOS/Android) and webview using JSON Schema and automatically generates SDK code

Defines the communication layer between mobile native(iOS/Android) and webview using JSON Schema and automatically generates SDK code.

당근마켓 31 Dec 8, 2022
Fast & tiny DOM differ

swapdom Fast & tiny DOM swapper. import swap from './swap-inflate.js' swap(parentNode, oldNodes, newNodes, endNode) swap-deflate.js strategy is small

Spect 9 Nov 29, 2022
Edit JSON text in-place for a minimal diff

edit-json Edit a textual JSON (not a JavaScript object) for a minimal diff, either programatically or by applying a JSON Patch (RFC6902). When seriali

Gustaf Räntilä 4 Oct 14, 2022
Pretty diff to html javascript library (diff2html)

diff2html diff2html generates pretty HTML diffs from git diff or unified diff output. Table of Contents Features Online Example Distributions Usage Di

Rodrigo Fernandes 2.3k Dec 31, 2022
Pretty diff to html javascript cli (diff2html-cli)

diff2html-cli Diff to Html generates pretty HTML diffs from unified and git diff output in your terminal Table of Contents Features Online Example Dis

Rodrigo Fernandes 404 Dec 19, 2022
Pretty diff to html javascript library (diff2html)

diff2html diff2html generates pretty HTML diffs from git diff or unified diff output. Table of Contents Features Online Example Distributions Usage Di

Rodrigo Fernandes 2.3k Dec 29, 2022
Get a diff view of your Obsidian Sync, File Recovery and Git version history

Version History Diff (for Sync and File Recovery Core plugins and Git) Note This plugin uses private APIs, so it may break at any time. Use at your ow

null 39 Dec 26, 2022
A simple image diff tool powered by avernakis.

Image Diff English | 简体中文 Introduction This is the tutorial project of Avernakis UI. Tiny, but production ready. Install Use npm as nam! (Node App Man

null 45 Dec 3, 2022
🔐 A Privacy-first Diff tool for everyone. ✅ Your data never hits our servers.

Features ?? Privacy-first developer tool. ?? Run on your Web Browser, Your data is sent nowhere. ?? Find a Difference between two text input for Side

Proxyman 41 Dec 5, 2022
⚠️ [Deprecated] No longer maintained, please use https://github.com/fengyuanchen/jquery-viewer

Viewer A simple jQuery image viewing plugin. As of v1.0.0, the core code of Viewer is replaced with Viewer.js. Demo Viewer.js - JavaScript image viewe

Fengyuan Chen 1k Dec 19, 2022
3D graph viewer powered by WebGL (three.js)

Graphosaurus A three-dimensional static graph viewer. (click the image to try it out) Demos EVE Online map Add nodes incrementally Documentation JSDoc

Corey Farwell 362 Dec 4, 2022
An opensource 360° media viewer written in JavaScript using Electron and Marzipano

open360viewer open360viewer is an opensource 360° media viewer. It is based on electron and marzipano. It currently supports opening equirectangular 3

null 4 Oct 9, 2022
Icon Ultimate Media Viewer

Ultimate Media Viewer View everything. Supported Images Audio Videos Documents Models PNG MP4 MD GLB JPG WEBM PDF GIF M3U8 SVG 3GP WEBP ICO Download R

null 1 May 23, 2022
Dead Simple Postgres Data Viewer and Query Runner

Dead Simple Postgres Data Viewer and Query Runner Environment Variables GITHUB_CLIENT_ID Github Client ID of the Oauth Application for protecting your

Mahesh C. Regmi 7 Aug 22, 2022
An alternative viewer for DevDAO NFT with a ✨holographic✨ twist

Holo DevDAO An alternative viewer for DevDAO NFT with a ✨ holographic ✨ twist. Pre-requisites node version >= 15.0.1 & npm >= 7.20.6. If you have nvm

Naomi Hauret 19 Dec 17, 2022