-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-explorer.js
37 lines (29 loc) · 1020 Bytes
/
json-explorer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
document.getElementById('apply').addEventListener('click', function() {
init(JSON.parse(document.getElementById('jsonInput').value), document.getElementById('jsonOutput'))
})
function init(obj, el) {
el.innerHTML = '';
el.className = "json-explorer"
el.addEventListener('click', function (e) {
if (e.target.classList.contains('parent')) {
e.target.classList.toggle('expanded')
}
})
function buildList(parentObj, parentEl) {
var rEl, rParent;
rParent = document.createElement('ul');
for (var key in parentObj) {
rEl = document.createElement('li');
if (typeof parentObj[key] == "object") {
rEl.className = parentObj[key] instanceof Array ? 'parent array' : 'parent';
rEl.innerHTML = '<span class="key">' + key + '</span>: {'
buildList(parentObj[key], rEl)
} else {
rEl.innerHTML = '<span class="key">' + key + '</span>: <span class="value">' + parentObj[key] + '</span>';
}
rParent.appendChild(rEl);
}
parentEl.appendChild(rParent);
}
buildList(obj, el);
}