{"id":3070,"date":"2018-11-11T23:46:08","date_gmt":"2018-11-11T23:46:08","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=3070"},"modified":"2018-11-12T02:45:39","modified_gmt":"2018-11-12T02:45:39","slug":"bash-jq-command-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/11\/bash-jq-command-linux-hint\/","title":{"rendered":"Bash jq command | Linux Hint"},"content":{"rendered":"<p>JSON data are used for various purposes. But JSON data can\u2019t be read easily from JSON file by using bash script like other normal files.<\/p>\n<p>jq<\/p>\n<p>tool is used to solve this problem.<\/p>\n<p>jq<\/p>\n<p>command works like<\/p>\n<p>sed<\/p>\n<p>and<\/p>\n<p>awk<\/p>\n<p>command, and it uses a domain specific language for working with JSON data.<\/p>\n<p>jq<\/p>\n<p>is not a built-in command. So, you have to install this command for using it. How you can install and apply<\/p>\n<p>jq<\/p>\n<p>command for reading or manipulating JSON data is shown in this tutorial.<\/p>\n<p>Run the following command to install jq on Ubuntu.<\/p>\n<p>$ sudo apt-get install jq<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"728\" height=\"423\" \/><\/p>\n<h2>Reading JSON data<\/h2>\n<p>Suppose, you have declared a JSON variable named JsonData in the terminal and run jq command with that variable to print the content of that variable.<\/p>\n<p>$ JsonData='[{&#8220;Book&#8221;:&#8221;PHP 7&#8243;}, {&#8220;Publication&#8221;:&#8221;Apress&#8221;},<br \/>\n{&#8220;Book&#8221;:&#8221;React 16 Essentials&#8221;},{&#8220;Publication&#8221;:&#8221;Packt&#8221;} ]&#8217;<br \/>\n$ echo &#8220;$&#8221; | jq &#8216;.&#8217;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"747\" height=\"350\" \/><\/p>\n<h3>Reading JSON data with \u2013c option<\/h3>\n<p>-c option uses with jq command to print each JSON object in each line. After running the following command, each object of JsonData variable will be printed.<\/p>\n<p>$ echo &#8220;$&#8221; | jq -c &#8216;.[]&#8217;<\/p>\n<h3>Reading a JSON file<\/h3>\n<p>jq command can be used for reading JSON file also. Create a JSON file named Students.json with the following content to test the next commands of this tutorial.<\/p>\n<h4>Students.json<\/h4>\n<p>[<\/p>\n<p>{&#8220;roll&#8221;: 3,<\/p>\n<p>&#8220;name&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;Micheal&#8221;,<br \/>\n&#8220;batch&#8221;<\/p>\n<p>:<\/p>\n<p>29,<br \/>\n&#8220;department&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;CSE&#8221;<\/p>\n<p>},{&#8220;roll&#8221;: 55,<\/p>\n<p>&#8220;name&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;Lisa&#8221;,<br \/>\n&#8220;batch&#8221;<\/p>\n<p>:<\/p>\n<p>34,<br \/>\n&#8220;department&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;BBA&#8221;<\/p>\n<p>},{&#8220;roll&#8221;: 12,<\/p>\n<p>&#8220;name&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;John&#8221;,<br \/>\n&#8220;batch&#8221;<\/p>\n<p>:<\/p>\n<p>22,<br \/>\n&#8220;department&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;English&#8221;<\/p>\n<p>}]<\/p>\n<p>Run the following command to read Students.json file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"739\" height=\"403\" \/><\/p>\n<h3>Reading JSON file with \u2018|\u2019<\/h3>\n<p>You can use \u2018|\u2019 symbol in the following way to read any JSON file.<\/p>\n<p>$ cat Students.json | jq &#8216;.&#8217;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"741\" height=\"411\" \/><\/p>\n<h3>Reading single key values<\/h3>\n<p>You can easily read any particular object from a JSON file by using jq command. In Students.json, there are four objects. These are roll, name, batch, and department. If you want to read the value of department key only from each record then run jq command in the following way.<\/p>\n<p>$ jq &#8216;.[] | .department&#8217; Students.json<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"741\" height=\"97\" \/><\/p>\n<h3>Reading multiple keys<\/h3>\n<p>If you want to read two or more object values from JSON data then mention the object names by separating comma (,) in the jq command. The following command will retrieve the values of name and department keys.<\/p>\n<p>$ jq &#8216;.[] | .name, .department&#8217; Students.json<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"738\" height=\"151\" \/><\/p>\n<h3>Remove key from JSON data<\/h3>\n<p>jq command is used not only for reading JSON data but also to display data by removing the particular key. The following command will print all key values of Students.json file by excluding batch key. map and del function are used in jq command to do the task.<\/p>\n<p>$ jq &#8216;map(del(.batch))&#8217; Students.json<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"737\" height=\"349\" \/><\/p>\n<h3>Mapping Values<\/h3>\n<p>Without deleting the key from JSON data, you can use map function with jq command for various purposes. Numeric values of JSON data can be increased or decreased by map function. Create a JSON file named Number.json with the following content to test the next commands.<\/p>\n<p>Run the following command to add 10 with each object value of Numbers,json.<\/p>\n<p>$ jq &#8216;map(.+10)&#8217; Numbers.json<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"739\" height=\"167\" \/><\/p>\n<p>Run the following command to subtract 10 from each object value of Numbers,json.<\/p>\n<p>$ jq &#8216;map(.-10)&#8217; Numbers.json<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"740\" height=\"174\" \/><\/p>\n<h3>Searching values by index and length<\/h3>\n<p>You can read objects from JSON file by specifying the particular index and length. Create a JSON file named colors.json with the following data.<\/p>\n<p>[&#8220;Red&#8221;,&#8221;Green&#8221;,&#8221;Blue&#8221;,&#8221;Yellow&#8221;,&#8221;Purple&#8221;]<\/p>\n<p>Run the following command to read two values starting from the third index of colors.json file.<\/p>\n<p>$ jq &#8216;.[2:4]&#8217; colors.json<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"732\" height=\"121\" \/><\/p>\n<p>You can specify the length or starting index to read data from JSON file. In the following example, the number of data value is given only. In this case, the command will read four data from the first index of colors.json.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"732\" height=\"156\" \/><\/p>\n<p>You can specify the starting point only without any length value in jq command and the value can be positive or negative. If the starting point is positive then the index will count from the left side of the list and starting from zero. If the starting point is negative then the index will count from the right side of the list and starting from one. In the following example, the starting point is -3. So, the last three values from the data will display.<\/p>\n<p>$ jq &#8216;.[-3:]&#8217; colors.json<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"734\" height=\"134\" \/><\/p>\n<p>When you will work with JSON data and want to parse or manipulate data according to your requirements then jq command will help you to make your task easier.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/bash_jq_command\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON data are used for various purposes. But JSON data can\u2019t be read easily from JSON file by using bash script like other normal files. jq tool is used to solve this problem. jq command works like sed and awk command, and it uses a domain specific language for working with JSON data. jq is &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/11\/bash-jq-command-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bash jq command | Linux Hint&#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-3070","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\/3070","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=3070"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3070\/revisions"}],"predecessor-version":[{"id":3271,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3070\/revisions\/3271"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=3070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=3070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=3070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}