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.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

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

Read the WSO2 Methodology for Agility to see how you can transform your integration projects from semi-agile to a scalable continuous agile approach.

json ,debugging

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}