How can I display JSON in an easy-to-read (for human readers) format? I'm looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc.
相关问题
- Is there a limit to how many levels you can nest i
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to toggle on Order in ReactJS
- StackExchange API - Deserialize Date in JSON Respo
Unsatisfied with other pretty printers for Ruby, I wrote my own (NeatJSON) and then ported it to JavaScript including a free online formatter. The code is free under MIT license (quite permissive).
Features (all optional):
I'll copy the source code here so that this is not just a link to a library, but I encourage you to go to the GitHub project page, as that will be kept up-to-date and the code below will not.
Here is how you can print without using native function.
Here's user123444555621's awesome HTML one adapted for terminals. Handy for debugging Node scripts:
Usage:
The simplest way to display an object for debugging purposes:
If you want to display the object in the DOM, you should consider that it could contain strings that would be interpreted as HTML. Therefore, you need to do some escaping...
This is nice:
https://github.com/mafintosh/json-markup from
mafintosh
HTML
Example stylesheet can be found here
Based on Pumbaa80's answer I have modified the code to use the console.log colours (working on Chrome for sure) and not HTML. Output can be seen inside console. You can edit the _variables inside the function adding some more styling.
Here is a bookmarklet you can use:
Usage:
Edit: I just tried to escape the % symbol with this line, after the variables declaration:
But I find out that Chrome is not supporting % escaping in the console. Strange... Maybe this will work in the future.
Cheers!