Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

The Integration Zone is brought to you in partnership with Cloud Elements.  What’s below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

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 -.

The State of API Integration Report provides data from the Cloud Elements platform and will help all developers navigate the recent explosion of APIs and the implications of API integrations to work more efficiently in 2017 and beyond.

Topics:
json ,debugging

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}