{"id":11660,"date":"2019-03-15T16:32:55","date_gmt":"2019-03-15T16:32:55","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=11660"},"modified":"2019-03-15T21:28:41","modified_gmt":"2019-03-15T21:28:41","slug":"how-to-parse-and-pretty-print-json-with-linux-commandline-tools","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/how-to-parse-and-pretty-print-json-with-linux-commandline-tools\/","title":{"rendered":"How To Parse And Pretty Print JSON With Linux Commandline Tools"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.ostechnix.com\/wp-content\/uploads\/2019\/03\/json-720x340.png\" alt=\"parse and pretty print json\" \/><\/p>\n<p><strong>JSON<\/strong>\u00a0is a lightweight and language independent data storage format, easy to integrate with most programming languages and also easy to understand by humans, of course when properly formatted. The word JSON stands for\u00a0<strong>J<\/strong>ava<strong>S<\/strong>cript\u00a0<strong>O<\/strong>bject\u00a0<strong>N<\/strong>otation, though it starts with JavaScript, and primarily used to exchange data between server and browser, but now being used in many fields including embedded systems. Here we\u2019re going to parse and pretty print JSON with command line tools on Linux. It\u2019s extremely useful for handling large JSON data in a shell scripts, or manipulating JSON data in a shell script.<\/p>\n<h4 class=\"fittexted_for_content_h4\" align=\"left\">What is pretty printing?<\/h4>\n<p align=\"left\">The JSON data is structured to be somewhat more human readable. However in most cases, JSON data is stored in a single line, even without a line ending character.<\/p>\n<p align=\"left\">Obviously that\u2019s not very convenient for reading and editing manually.<\/p>\n<p align=\"left\">That\u2019s when pretty print is useful. The name is quite self explanatory, re-formatting the JSON text to be more legible by humans. This is known as\u00a0<strong>JSON pretty printing<\/strong>.<\/p>\n<h4 class=\"fittexted_for_content_h4\" align=\"left\">Parse And Pretty Print JSON With Linux Commandline Tools<\/h4>\n<p align=\"left\">JSON data could be parsed with command line text processors like\u00a0<strong>awk<\/strong>,\u00a0<strong>sed<\/strong>\u00a0and\u00a0<strong>gerp<\/strong>. In fact JSON.awk is an awk script to do that. However there are some dedicated tools for the same purpose.<\/p>\n<ol>\n<li>\n<p align=\"left\"><strong>jq<\/strong>\u00a0or\u00a0<strong>jshon<\/strong>, JSON parser for shell, both of them are quite useful.<\/p>\n<\/li>\n<li>\n<p align=\"left\">Shell scripts like\u00a0<strong>JSON.sh<\/strong>\u00a0or\u00a0<strong>jsonv.sh<\/strong>\u00a0to parse JSON in bash, zsh or dash shell.<\/p>\n<\/li>\n<li>\n<p align=\"left\"><strong>JSON.awk<\/strong>, JSON parser awk script.<\/p>\n<\/li>\n<li>\n<p align=\"left\">Python modules like\u00a0<strong>json.tool<\/strong>.<\/p>\n<\/li>\n<li>\n<p align=\"left\"><strong>underscore-cli<\/strong>, Node.js and javascript based.<\/p>\n<\/li>\n<\/ol>\n<p align=\"left\">In this tutorial I\u2019m focusing only on\u00a0<strong>jq<\/strong>, which is quite powerful JSON parser for shells with advanced filtering and scripting capability.<\/p>\n<h4 class=\"fittexted_for_content_h4\" align=\"left\">JSON pretty printing<\/h4>\n<p align=\"left\">JSON data could be in one and nearly illegible for humans, so to make it somewhat readable, JSON pretty printing is here.<\/p>\n<p align=\"left\"><strong>Example:<\/strong>\u00a0A data from\u00a0<strong>jsonip.com<\/strong>, to get external IP address in JSON format, use\u00a0<strong>curl<\/strong>\u00a0or\u00a0<strong>wget<\/strong>\u00a0tools like below.<\/p>\n<pre>$ wget -cq http:\/\/jsonip.com\/ -O -<\/pre>\n<p align=\"left\">The actual data looks like this:<\/p>\n<pre>{\"ip\":\"111.222.333.444\",\"about\":\"\/about\",\"Pro!\":\"http:\/\/getjsonip.com\"}<\/pre>\n<p align=\"left\">Now pretty print it with jq:<\/p>\n<pre>$ wget -cq http:\/\/jsonip.com\/ -O - | jq '.'<\/pre>\n<p align=\"left\">This should look like below, after filtering the result with jq.<\/p>\n<pre>{\r\n\r\n   \"ip\": \"111.222.333.444\",\r\n\r\n   \"about\": \"\/about\",\r\n\r\n   \"Pro!\": \"http:\/\/getjsonip.com\"\r\n\r\n}<\/pre>\n<p align=\"left\">The Same thing could be done with python\u00a0<strong>json.tool<\/strong>\u00a0module. Here is an example:<\/p>\n<pre>$ cat anything.json | python -m json.tool<\/pre>\n<p align=\"left\">This Python based solution should be fine for most users, but it\u2019s not that useful where Python is not pre-installed or could not be installed, like on embedded systems.<\/p>\n<p align=\"left\">However the json.tool python module has a distinct advantage, it\u2019s cross platform. So, you can use it seamlessly on Windows, Linux or mac OS.<\/p>\n<hr \/>\n<p><strong>Suggested read:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/www.ostechnix.com\/prettyping-make-the-output-of-ping-command-prettier-and-easier-to-read\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Prettyping \u2013 Make The Output Of Ping Command Prettier And Easier To Read<\/strong><\/a><\/li>\n<\/ul>\n<hr \/>\n<h4 class=\"fittexted_for_content_h4\">How to parse JSON with jq<\/h4>\n<p align=\"left\">First, you need to install jq, it\u2019s already picked up by most GNU\/Linux distributions, install it with their respective package installer commands.<\/p>\n<p align=\"left\">On Arch Linux:<\/p>\n<pre>$ sudo pacman -S jq<\/pre>\n<p align=\"left\">On Debian, Ubuntu, Linux Mint:<\/p>\n<pre>$ sudo apt-get install jq<\/pre>\n<p align=\"left\">On Fedora:<\/p>\n<pre>$ sudo dnf install jq<\/pre>\n<p align=\"left\">On openSUSE:<\/p>\n<pre>$ sudo zypper install jq<\/pre>\n<p align=\"left\">For other OS or platforms, see the\u00a0<a href=\"https:\/\/stedolan.github.io\/jq\/download\/\" target=\"_blank\" rel=\"noopener\">official installation instructions<\/a>.<\/p>\n<p align=\"left\"><strong>Basic filters and identifiers of jq<\/strong><\/p>\n<p align=\"left\">jq could read the JSON data either from\u00a0<strong>stdin<\/strong>\u00a0or a\u00a0<strong>file<\/strong>. You\u2019ve to use both depending on the situation.<\/p>\n<p align=\"left\">The single symbol of\u00a0<strong>.<\/strong>\u00a0is the most basic filter. These filters are also called as\u00a0<strong>object identifier-index<\/strong>. Using a single\u00a0<strong>.\u00a0<\/strong>along with jq basically pretty prints the input JSON file.<\/p>\n<p align=\"left\"><strong>Single quotes<\/strong>\u00a0\u2013 You don\u2019t have to use the single quote always. But if you\u2019re combining several filters in a single line, then you must use them.<\/p>\n<p align=\"left\"><strong>Double quotes<\/strong>\u00a0\u2013 You\u2019ve to enclose any special character like\u00a0<strong>@<\/strong>,\u00a0<strong>#<\/strong>,\u00a0<strong>$<\/strong>\u00a0within two double quotes, like this example,\u00a0<strong>jq .foo.\u201d@bar\u201d<\/strong><\/p>\n<p align=\"left\"><strong>Raw data print<\/strong>\u00a0\u2013 For any reason, if you need only the final parsed data, not enclosed within a double quote, use the -r flag with the jq command, like this.\u00a0<strong>\u2013 jq -r .foo.bar<\/strong>.<\/p>\n<p align=\"left\"><strong>Parsing specific data<\/strong><\/p>\n<p align=\"left\">To filter out a specific part of JSON, you\u2019ve to look into the pretty printed JSON file\u2019s data hierarchy.<\/p>\n<p align=\"left\">An example of JSON data, from Wikipedia:<\/p>\n<pre>{\r\n\r\n  \"firstName\": \"John\",\r\n\r\n  \"lastName\": \"Smith\",\r\n\r\n  \"age\": 25,\r\n\r\n  \"address\": {\r\n\r\n    \"streetAddress\": \"21 2nd Street\",\r\n\r\n    \"city\": \"New York\",\r\n\r\n    \"state\": \"NY\",\r\n\r\n    \"postalCode\": \"10021\"\r\n\r\n},\r\n\r\n  \"phoneNumber\": [\r\n\r\n{\r\n\r\n  \"type\": \"home\",\r\n\r\n  \"number\": \"212 555-1234\"\r\n\r\n},\r\n\r\n{\r\n\r\n  \"type\": \"fax\",\r\n\r\n  \"number\": \"646 555-4567\"\r\n\r\n}\r\n\r\n],\r\n\r\n  \"gender\": {\r\n\r\n  \"type\": \"male\"\r\n\r\n  }\r\n\r\n}<\/pre>\n<p align=\"left\">I\u2019m going to use this JSON data as an example in this tutorial, saved this as\u00a0<strong>sample.json<\/strong>.<\/p>\n<p align=\"left\">Let\u2019s say I want to filter out the address from sample.json file. So the command should be like:<\/p>\n<pre>$ jq .address sample.json<\/pre>\n<p align=\"left\"><strong>Sample output:<\/strong><\/p>\n<pre>{\r\n\r\n  \"streetAddress\": \"21 2nd Street\",\r\n\r\n  \"city\": \"New York\",\r\n\r\n  \"state\": \"NY\",\r\n\r\n  \"postalCode\": \"10021\"\r\n\r\n}<\/pre>\n<p align=\"left\">Again let\u2019s say I want the postal code, then I\u2019ve to add another\u00a0<strong>object identifier-index<\/strong>, i.e. another filter.<\/p>\n<pre>$ cat sample.json | jq .address.postalCode<\/pre>\n<p align=\"left\">Also note that the\u00a0<strong>filters are case sensitive<\/strong>\u00a0and you\u2019ve to use the exact same string to get something meaningful output instead of null.<\/p>\n<p align=\"left\"><strong>Parsing elements from JSON array<\/strong><\/p>\n<p align=\"left\">Elements of JSON array are enclosed within square brackets, undoubtedly quite versatile to use.<\/p>\n<p align=\"left\">To parse elements from a array, you\u2019ve to use the\u00a0<strong>[]identifier<\/strong>\u00a0along with other object identifier-index.<\/p>\n<p align=\"left\">In this sample JSON data, the phone numbers are stored inside an array, to get all the contents from this array, you\u2019ve to use only the brackets, like this example.<\/p>\n<pre>$ jq .phoneNumber[] sample.json<\/pre>\n<p align=\"left\">Let\u2019s say you just want the first element of the array, then use the array object numbers starting for 0, for the first item, use\u00a0<strong>[0]<\/strong>, for the next items, it should be incremented by one each step.<\/p>\n<pre>$ jq .phoneNumber[0] sample.json<\/pre>\n<p align=\"left\"><strong>Scripting examples<\/strong><\/p>\n<p align=\"left\">Let\u2019s say I want only the the number for home, not entire JSON array data. Here\u2019s when scripting within jq command comes handy.<\/p>\n<pre>$ cat sample.json | jq -r '.phoneNumber[] | select(.type == \"home\") | .number'<\/pre>\n<p align=\"left\">Here first I\u2019m piping the results of one filer to another, then using the select attribute to select a particular type of data, again piping the result to another filter.<\/p>\n<p align=\"left\">Explaining every type of jq filters and scripting is beyond the scope and purpose of this tutorial. It\u2019s highly suggested to read the JQ manual for better understanding given below.<\/p>\n<p align=\"left\"><strong>Resources:<\/strong><\/p>\n<ul>\n<li><strong><a href=\"https:\/\/stedolan.github.io\/jq\/manual\/\" target=\"_blank\" rel=\"noopener\">https:\/\/stedolan.github.io\/jq\/manual\/<\/a><\/strong><\/li>\n<li><strong><a href=\"http:\/\/www.compciv.org\/recipes\/cli\/jq-for-parsing-json\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.compciv.org\/recipes\/cli\/jq-for-parsing-json\/<\/a><\/strong><\/li>\n<li><a href=\"https:\/\/lzone.de\/cheat-sheet\/jq\" target=\"_blank\" rel=\"noopener\"><strong>https:\/\/lzone.de\/cheat-sheet\/jq<\/strong><\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.ostechnix.com\/how-to-parse-and-pretty-print-json-with-linux-commandline-tools\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON\u00a0is a lightweight and language independent data storage format, easy to integrate with most programming languages and also easy to understand by humans, of course when properly formatted. The word JSON stands for\u00a0JavaScript\u00a0Object\u00a0Notation, though it starts with JavaScript, and primarily used to exchange data between server and browser, but now being used in many fields &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/how-to-parse-and-pretty-print-json-with-linux-commandline-tools\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How To Parse And Pretty Print JSON With Linux Commandline Tools&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11660","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11660"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11660\/revisions"}],"predecessor-version":[{"id":11675,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11660\/revisions\/11675"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}