Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Pretty Print JSON Data In HTML In Color Code Using JavaScript

Pretty Print JSON Data In HTML In Color Code Using JavaScript

So many times we are required to print the JSON in pretty format because it is easy to read and understood. As we know JSON (JavaScript object notation) is a lightweight data-interchange format. It is easy for humans to read and write. This tutorial shows how to pretty print JSON data in HTML in colour code using javascript

Javascript JSON Pretty Print Tutorial Shows How To Pretty Print JSON Data In Javascript. Pretty Printing Is A Form Of Stylistic Formatting Including Indentation And Colouring.

This is a simple frontend utility to help the file-upload process on your website. It is written in pure JavaScript, has no dependencies. You can check out the live demo. For the most part, browsers do a good job of handling image uploads. That being said – we find that the ability to show our users a preview of their upload can go a long way in increasing the confidence in their upload.


  1. Light Weight.
  2. Pure JavaScrit.
  3. Cross Browser.
  4. One Function To Call

How To Pretty Print JSON Data In HTML In Color Code Using JavaScript?

There are few easy and understandable steps to achieve your desired functionality using pure Vanilla JavaScript that we are gonna share below. Follow each step perfectly.

Recommended For You:
Get All Resources Like href And src From A WebPage Using PHP


pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }


function syntaxHighlight(json) {
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>';

var obj = {a:1, 'b':'foo', c:[false,'false',null, 'null', {d:{e:1.3e5,f:'1.3e5'}}]};
var str = JSON.stringify(obj, undefined, 4);



No need to customize it. Just copy paste.

Troubleshooting the Errors

Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us or If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

Recommended For You:
Pure JavaScript Auto Filter Continent, Country, City HTML Dropdown List

You Like It, Please Share This Recipe With Your Friends Using...


Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *