react-diff-viewer icon indicating copy to clipboard operation
react-diff-viewer copied to clipboard

include Support for JSON compare

Open saikrishnaakula opened this issue 5 years ago • 5 comments
trafficstars

Library only work for Text can we add support for JSON

saikrishnaakula avatar Oct 13 '20 11:10 saikrishnaakula

@saikrishnaakula, did you find another react component to compare jsons ?

Jav3k avatar Oct 22 '20 07:10 Jav3k

Not sure if this will help you but I did this to get around it.

  <ReactDiffViewer
    oldValue={JSON.stringify(oldJson, null, 2)}
    newValue={JSON.stringify(newJson, null, 2)}
    splitView
 />

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

dannyseo517 avatar Oct 30 '20 17:10 dannyseo517

@kaniseo, yeah. but you can also sort json fields and values and compare them with DiffMethod.LINES

Jav3k avatar Oct 31 '20 07:10 Jav3k

Thank you, Definitly it will help but we have to take care of sorting json by keys

Not sure if this will help you but I did this to get around it.

  <ReactDiffViewer
    oldValue={JSON.stringify(oldJson, null, 2)}
    newValue={JSON.stringify(newJson, null, 2)}
    splitView
 />

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

saikrishnaakula avatar Nov 01 '20 10:11 saikrishnaakula

I worked around the field sorting by using https://github.com/substack/json-stable-stringify and passing in the result to ReactDiffViewer, like this:

import * as stableStringify from "json-stable-stringify";
        <ReactDiffViewer
          oldValue={stableStringify(result.before, {space: "  "})}
          newValue={stableStringify(result.after, {space: "  "})}
          compareMethod={DiffMethod.LINES}
          splitView={true}
        />

joshzana avatar Nov 15 '20 21:11 joshzana