in Technology by
How to display JSON in an easy-to-read (for human readers) format?

1 Answer

0 votes
by
Pretty-printing is implemented natively in JSON.stringify(). The third argument enables pretty printing and sets the spacing to use:


var str = JSON.stringify(obj, null, 2); // spacing level = 2

If you need syntax highlighting, you might use some regex magic like so:

function syntaxHighlight(json) {
    if (typeof json != 'string') {
         json = JSON.stringify(json, undefined, 2);
    }
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

Related questions

0 votes
    here is my code. var http=require("http"); var fs = require("fs"); var express = require(" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    Which of the following produces hypotheses that are easy to read for humans? a) Machine Learning b) ILP c) First-order logic d) Propositional logic...
asked Jan 15, 2023 in Education by JackTerrance
0 votes
    How to format a Microsoft JSON date?...
asked Jan 11, 2021 in Technology by JackTerrance
0 votes
    How to pass json format data on ajax call?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    State wheter the following statements are true or false. larger fonts catch reader’s attention Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    I been receiving date in the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format from JSON input, which gets ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I am working in an graphql application where I have to send custom error object / message in json ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I make a website on php and want to read some information from text file. the reason is if, i ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I make a website on php and want to read some information from text file. the reason is if, i ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    _______is the syntax to read JSON data?...
asked Mar 4, 2023 in Technology by JackTerrance
0 votes
    I have huge JSON files in a folder approx 200 each of 300 mbs approx. I need to write Spring ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    1. Write a function in python to read the content from a text file “poem.txt” line by line and display the same on screen Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    Is there an easy way to sift through your guest users?...
asked Mar 10, 2021 in Technology by JackTerrance
0 votes
    The _taskbar is more flexible and provides an easy navigation facility Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    a _ is a representation of data in a table in an easy to understand Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
...