Over a million developers have joined DZone.

Pretty Printing JSON Documents for Further Processing

DZone's Guide to

Pretty Printing JSON Documents for Further Processing

Many browser add-ons lack the power of command line and capabilities of text editors. A simple pipe to a Python module makes JSON much more readable.

Free Resource

Discover how Microservices are a type of software architecture where large applications are made up of small, self-contained units working together through APIs that are not dependent on a specific language. Brought to you in partnership with AppDynamics.

APIs are primarily designed for computers, not for humans. Yet sometimes, you need to dig into raw response from such APIs. If such a response (let’s assume you’re interested only in its payload) happens to be an unformatted JSON document, then debugging it won’t be a pleasant experience unless you format it.

There are many browser add-ons such as Postman and HttpRequester that allow you to view pretty printed responses. However, they all lack the power of command line and capabilities of text editors, not to mention that they make you leave the command line and click over a GUI. Of course, browser add-ons also provide useful features (i.e., request history) that make you more productive, but for this article, we’re going to assume that our case is easier to solve without leaving the command line or text editor.

Let’s take a look at a simple response from GitHub Status API. Looking at the raw payload doesn’t feel friendly:

$ curl -s https://status.github.com/api/messages.json
[{"status":"good","body":"Everything operating normally.","created_on":"2015-12-29T17:25:32Z"},{"status":"minor","body":"We're investigating increased error rates with some git operations.","created_on":"2015-12-29T17:06:54Z"}]

But a simple pipe to a Python module makes it much more readable...

$ curl -s https://status.github.com/api/messages.json | python -m json.tool
        "body": "Everything operating normally.",
        "created_on": "2015-12-29T17:25:32Z",
        "status": "good"
        "body": "We're investigating increased error rates with some git operations.",
        "created_on": "2015-12-29T17:06:54Z",
        "status": "minor"

...and moreover allows you to pipe it further.

So simple, yet so powerful. If you need to go over a record of such requests or just need to use a text editor, it doesn’t get more complicated. You know what I mean if you’re a vim user; you just execute %!python -m json.tool in command-line mode.

Formatting JSON documents in vim

And if you’ve been using vim for years, yet have not been able to exit it, you still have other options. For example, gedit allows you to execute external tools once you enable the External Tools plugin. All you need to do in that case is create a new external command and then execute it.

Defining format JSON command in External Tools Manager
Executing format JSON command

And as in the case of creating an alias for python -m json.tool or mapping a key in vim, you can also well assign a shortcut for that command in gedit.

P.S.—If you need to debug unformatted XML documents, follow the same route with xmllint --format -.

Discover the six challenges and best practices in managing microservice performance, brought to you in partnership with AppDynamics.

json ,debugging

Published at DZone with permission of Kamil Szymański. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}