<!DOCTYPE html>
<!-- saved from url=(0034)https://onlinetextcompare.com/json -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="theme-color" content="#08C988">
<meta property="og:locale" content="en_US">
<meta property="og:type" content="website">
<meta property="og:site_name" content="Online Text Compare">
<title>Compare JSON</title>
<meta name="keywords" content="JSON Compare">
<link rel="canonical" href="https://onlinetextcompare.com/json">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="./CompareJSON/ccsiteV6.css">
<link rel="stylesheet" type="text/css" href="./CompareJSON/codemirror.css">
<link rel="stylesheet" type="text/css" href="./CompareJSON/merge.css">
<script src="./CompareJSON/codemirror.js"></script>
<script src="./CompareJSON/diff_match_patch.js"></script>
<script src="./CompareJSON/merge.js"></script>
<script src="./CompareJSON/javascript.js"></script>
<!--Analytics-->
<style>
#view {
width: 90%;
}
#container:fullscreen #view {
width: 100%;
}
#container:fullscreen .CodeMirror-merge, #container:fullscreen .CodeMirror-merge .CodeMirror {
height: 95vh;
}
#viewHeader {
width:90%;padding:10px;margin:8px auto 16px auto;text-align:center;
}
@media (max-width: 1250px) {
#fs { visibility: hidden; }
#viewHeader {margin:auto;text-align:center;height:40px;}
}
</style>
</head>
<body>
<div class="intro" style="max-width: 47.625rem; margin: 0 auto; text-align:center">
<div class="wrap"><h1 style="margin: 3.5rem 0 0.5rem 0;">JSON Compare</h1></div>
<p>Find differences between two JSON Queries</p>
<button id="btn-format" class="write1">Format & Compare JSON</button>
<br> <br>
</div>
<div id="container" style="background-color: white;">
<div style="margin: 0 auto; text-align:center">
<b>Sources</b>
</div>
<div id="source" style="width:90%;margin:auto;border:1px solid #7a7a7a">
</div>
<div id="viewHeader">
<div style="width:45%;float:left;">
<b>Before JSON file:</b> Paste contents or <input id="firstfile" type="file">
</div>
<div style="width:45%;float:right;">
<b style="padding-top: 10px;">After JSON file:</b> Paste contents or <input id="secondfile" type="file">
<img src="./CompareJSON/expand.svg" id="fs" title="Toggle Fullscreen" style="vertical-align: middle;">
</div>
</div>
<div id="view" style="padding:10px;margin:auto;">
</div>
</div>
<script type="text/javascript">
var source = document.getElementById("source");
var sv = CodeMirror(source,{
lineNumbers: true,
mode: "application/json"
});
function sortSortJsonStr( json , replacer, space ) {
var jsonArr = Object.keys(json);
jsonArr = jsonArr.sort()
var leftSortJson = {};
jsonArr.forEach(function(cval){
leftSortJson[cval] = json[cval]
})
console.log(leftSortJson)
return JSON.stringify(leftSortJson, replacer, space);
}
var leftContent = "{}";
var rightContent = "{}";
var target = document.getElementById("view");
var mv = CodeMirror.MergeView(target, {
value: leftContent,
origLeft: null,
orig: rightContent,
lineNumbers: true,
mode: "application/json",
showDifferences: true,
connect: 'align',
collapseIdentical: false,
revertButtons: true,
allowEditingOriginals: true,
chunkClassLocation: ['background', 'wrap', 'gutter']
});
document.getElementById('btn-format').onclick = function() {
var source = JSON.parse(sv.getValue());
sv.setValue(sortSortJsonStr(source, undefined, 4));
var leftContenct = source && source.before ? source.before : "";
leftContenct = sortSortJsonStr(leftContenct, undefined, 4);
mv.editor().setValue(leftContenct);
var rightContent = source && source.after ? source.after : "";
rightContent = sortSortJsonStr(rightContent, undefined, 4);
mv.rightOriginal().setValue(rightContent);
};
let input = document.querySelector('#firstfile')
input.addEventListener('change', () => {
let files = input.files;
if (files.length == 0) return;
const file = files[0];
let reader = new FileReader();
reader.onload = (e) => {
const file = e.target.result;
mv.editor().setValue(file);
};
reader.onerror = (e) => alert(e.target.error.name);
reader.readAsText(file);
});
let input2 = document.querySelector('#secondfile')
input2.addEventListener('change', () => {
let files = input2.files;
if (files.length == 0) return;
const file = files[0];
let reader = new FileReader();
reader.onload = (e) => {
const file = e.target.result;
mv.rightOriginal().setValue(file);
};
reader.onerror = (e) => alert(e.target.error.name);
reader.readAsText(file);
});
let fullscreen = document.getElementById('container');
let button = document.getElementById('fs');
button.addEventListener("click", () => {
if (!document.fullscreenElement) {
fullscreen?.requestFullscreen();
} else {
document.exitFullscreen();
}
});
</script>
</body></html>