LATEST >>

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

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

Pretty Print JSON Data Formatter In HTML Using PHP

Pretty-Print-JSON-Data-Formatter-In-HTML-Using-PHP
So many times we are required to print the JSON in a 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 using PHP.

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

There are many code snippets available online or on many other blogs and websites but everyone is not able to optimize your blog or website so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

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

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

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:
Simple And Small Size Pure JavaScript Unlimited Tabs Accordion

Input:

{"key1":[1,2,3],"key2":"value"}

How To Call?:

print_r(prettyPrint($json));)

Output:

{
    "key1": [
        1,
        2,
        3
    ],
    "key2": "value"
}

PHP Function:

function prettyPrint( $json )
{
    $result = '';
    $level = 0;
    $in_quotes = false;
    $in_escape = false;
    $ends_line_level = NULL;
    $json_length = strlen( $json );

    for( $i = 0; $i < $json_length; $i++ ) {
        $char = $json[$i];
        $new_line_level = NULL;
        $post = "";
        if( $ends_line_level !== NULL ) {
            $new_line_level = $ends_line_level;
            $ends_line_level = NULL;
        }
        if ( $in_escape ) {
            $in_escape = false;
        } else if( $char === '"' ) {
            $in_quotes = !$in_quotes;
        } else if( ! $in_quotes ) {
            switch( $char ) {
                case '}': case ']':
                    $level--;
                    $ends_line_level = NULL;
                    $new_line_level = $level;
                    break;

                case '{': case '[':
                    $level++;
                case ',':
                    $ends_line_level = $level;
                    break;

                case ':':
                    $post = " ";
                    break;

                case " ": case "\t": case "\n": case "\r":
                    $char = "";
                    $ends_line_level = $new_line_level;
                    $new_line_level = NULL;
                    break;
            }
        } else if ( $char === '\\' ) {
            $in_escape = true;
        }
        if( $new_line_level !== NULL ) {
            $result .= "\n".str_repeat( "\t", $new_line_level );
        }
        $result .= $char.$post;
    }

    return $result;
}

Customization:

No need to customize it. Just copy and paste.

Troubleshooting the Errors

Do it with concentration and patience. Check your alls steps 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 If you have any doubts or 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:
Top 5 Awesome JavaScript Snippets For Common Tasks

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

2 Responses to “Pretty Print JSON Data Formatter In HTML Using PHP”

  1. Shivam says:

    I have face difficulting in placing JSON. Thanks

Leave a Reply

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