Created
December 23, 2021 11:24
-
-
Save ifgeny87/d844f71628ac818839e524eb2294ca39 to your computer and use it in GitHub Desktop.
JSON viewer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html> | |
| <head> | |
| <style> | |
| #output { | |
| position: fixed; | |
| top: 0; | |
| left: 0; | |
| right: 0; | |
| bottom: 20%; | |
| overflow: auto; | |
| } | |
| #output pre { | |
| margin: 0; | |
| padding: 0; | |
| font: 12pt Courier New; | |
| } | |
| #input { | |
| position: fixed; | |
| top: 80%; | |
| left: 0; | |
| right: 0; | |
| width: 100%; | |
| bottom: 0; | |
| overflow: auto; | |
| font: 12pt Courier New; | |
| } | |
| .child { | |
| padding-left: 10px; | |
| } | |
| .array { | |
| background-color: rgba(255, 200, 0, 0.1); | |
| } | |
| .object { | |
| background-color: rgba(0, 200, 255, 0.1); | |
| } | |
| .key { | |
| font-weight: bold; | |
| } | |
| .number { | |
| color: blue; | |
| font-weight: bold; | |
| } | |
| .string { | |
| color: green; | |
| } | |
| .boolean { | |
| color: fuchsia; | |
| } | |
| .null { | |
| color: gray; | |
| font-style: italic; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="output"> | |
| <pre>Вставь свой JSON ниже</pre> | |
| </div> | |
| <textarea id="input" placeholder="Сюда вставь свой JSON"></textarea> | |
| <script> | |
| const output = document.querySelector('#output pre'); | |
| const input = document.getElementById('input'); | |
| input.addEventListener('change', onChange); | |
| input.addEventListener('input', onChange); | |
| let lastText = ''; | |
| let changeTimerId; | |
| function onChange({ target: { value: text } }) { | |
| if (text === lastText) return; | |
| clearTimeout(changeTimerId); | |
| lastText = text; | |
| changeTimerId = setTimeout(updateOutput, 500); | |
| } | |
| function updateOutput() { | |
| const json = readJson(lastText); | |
| output.innerHTML = formatJson(json); | |
| } | |
| function readJson(s, index = 0) { | |
| let res; | |
| try { | |
| const o = JSON.parse(s); | |
| if (o instanceof Array) { | |
| res = o.map(os => readJson(os, index + 1)); | |
| } else if (typeof o === 'object') { | |
| Object.keys(o).forEach(key => o[key] = readJson(o[key], index + 1)); | |
| res = o; | |
| } else { | |
| res = o; | |
| } | |
| } catch (e) { | |
| if (typeof s === 'string') { | |
| const ss = s.split('\n'); | |
| if (ss.length < 2) { | |
| res = s; | |
| } else { | |
| res = ss.map(t => readJson(t.replace(/\\\\/g, '\\'), index + 1)); | |
| } | |
| } else { | |
| res = s; | |
| } | |
| } | |
| return res; | |
| } | |
| function formatJson(value, spaces = '') { | |
| let html = ''; | |
| if (value === null) { | |
| // null | |
| html = '<span class="null">null</span>'; | |
| } else if (typeof value === 'string') { | |
| // string | |
| html = `<span class="string">${JSON.stringify(value)}</span>`; | |
| } else if (typeof value === 'number') { | |
| // number | |
| html = `<span class="number">${value}</span>`; | |
| } else if (typeof value === 'boolean') { | |
| // boolean | |
| html = `<span class="boolean">${value}</span>`; | |
| } else if (value instanceof Array) { | |
| // Array | |
| html = `[\n`; | |
| html += value.map(v => `${spaces} ${formatJson(v, `${spaces} `)}`).join(',\n'); | |
| html += `\n${spaces}]`; | |
| } else if (typeof value === 'object') { | |
| // Object | |
| html = `{\n`; | |
| const keys = Object.keys(value); | |
| keys.forEach((key, index) => { | |
| const s = formatJson(value[key], `${spaces} `); | |
| html += `${spaces} <span class="key">${key}</span>: ${s}`; | |
| if (index < keys.length - 1) { | |
| html += ',\n'; | |
| } | |
| }); | |
| html += `\n${spaces}}`; | |
| } else { | |
| // unknown type | |
| throw new Error(`Bad format value ${value}`); | |
| } | |
| return html; | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment