Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Format JSON data on Ubuntu

  sonic0002        2016-08-17 11:05:09       25,602        1    

JSON now becomes a very popular data format because of its simplicity and light-weight. Nowadays many RESTful APIs will offer a choice of exchanging JSON data between the server and client. Sometimes the data may not be formatted and it cannot be easily read by human beings. It's frequently desired that the unformatted JSON data should be formatted before read.

Today we will show a few ways to format JSON data on Ubuntu. Assume we have a json file test.json with below content.

{ "title": "Test", "description": "Sample description" }

The normal cat command will output below content.

postman@postman-ubuntu:~/Work$ cat test.json
{ "title": "Test", "description": "Sample description" }

Now let's see a few commands or tools which can be used to format the JSON data.

jq

jq is a JSON preprocessor which can process JSON data with different filters. The simplest filter is . which will just do a normal formatting of the JSON data. This tool can be first installed with below command.

sudo apt-get install jq

Post that, run below command to output the formatted JSON data.

postman@postman-ubuntu:~/Work$ jq . test.json
{
  "description": "Sample description",
  "title": "Test"
}

json_xs

json_xs is a Linux command tool to convert between some input and output formats. One of them is JSON. To install it, use apt.

sudo apt-get install libjson-perl

The output of the command is

postman@postman-ubuntu:~/Work$ cat test.json | json_xs
{
   "title" : "Test",
   "description" : "Sample description"
}

YAJL

YAJL is a small command line tool written in C and it can provides basic JSON format capabilities. Install it with below command

sudo apt-get install yajl-tools

The command to format the JSON data is

postman@postman-ubuntu:~/Work$ cat test.json | json_reformat
{
  "title": "Test",
  "description": "Sample description"
}

The limitation of this tool is that it cannot take file as input, it can only take standard input.

json_pp

It's copied form json_xs with some modifications.

postman@postman-ubuntu:~/Work$ cat test.json | json_pp
{
   "title" : "Test",
   "description" : "Sample description"
}

Python

Using json.tool from Python can also format the JSON data.

postman@postman-ubuntu:~/Work$ python -m json.tool test.json
{
    "description": "Sample description",
    "title": "Test"
}

Ruby

A JSON package can be installed.

sudo apt-get install ruby-json-pure

Then run the prettify_json.rb.

postman@postman-ubuntu:~/Work$ prettify_json.rb test.json
{
  "title": "Test",
  "description": "Sample description"
}

Or jsonpretty gem can be used. To install the gem, run below.

gem install jsonpretty

Then run below command to format the JSON data.

postman@postman-ubuntu:~/Work$ cat test.json | jsonpretty
{
  "title": "Test",
  "description": "Sample description"
}

Or ppjson gem can be used as well. Install it with below command

gem install ppjson

Then format JSON data with below

postman@postman-ubuntu:~/Work$ ppjson -f test.json
{
  "title": "Test",
  "description": "Sample description"
}

NodeJS

NodeJS also can be used to format JSON data. To install NodeJS, below command can be ran.

sudo apt-get install nodejs

Then format the JSON data with below command.

postman@postman-ubuntu:~/Work$ node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync(process.argv[1])), null, 4));" test.json
{
    "title": "Test",
    "description": "Sample description"
}

There is also a json package written in JavaScript which depends on NodeJS.  It can be used to format JSON data. To install it,, have npm installed and then run below command to install json.

npm install -g json

Then prettify the JSON data with below

postman@postman-ubuntu:~/Work$ json -f test.json
{
  "title": "Test",
  "description": "Sample description"
}

UBUNTU  LINUX  RUBY  PYTHON  NODEJS  PERL  JSON  JQ  YAJL 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  1 COMMENT


Anonymous [Reply]@ 2020-06-20 09:22:25

great tutorial. Thank you