{"id":11130,"date":"2013-12-13T12:50:17","date_gmt":"2013-12-13T07:20:17","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11130"},"modified":"2016-12-19T15:07:28","modified_gmt":"2016-12-19T09:37:28","slug":"how-to-parse-json-by-command-line-in-linux","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-parse-json-by-command-line-in-linux\/","title":{"rendered":"How to parse JSON by command Line in Linux"},"content":{"rendered":"<p>Java development is often challenging, especially when you need to parse JSON from the command line. In my recent<span style=\"color: #445263\">\u00a0<a title=\"java development services\" href=\"http:\/\/www.tothenew.com\/java-development-services\">Java development project<\/a>\u00a0I had\u00a0<\/span><strong>parse JSON from the command line\u00a0<\/strong>and I explored<span style=\"color: #445263\">\u00a0available options one-by-one to find a close fit solution which can be implemented quickly.\u00a0<\/span><\/p>\n<p><span style=\"text-decoration: underline\"><strong>Requirements<\/strong><\/span><\/p>\n<p>To parse json from the command line you&#8217;ll need a software called <a href=\"http:\/\/stedolan.github.io\/jq\/download\/\">jq<\/a>\u00a0which is a command line JSON processor.<\/p>\n<p><span style=\"text-decoration: underline\"><strong>Installation<\/strong><\/span><\/p>\n<p><strong>Step1: <\/strong>Download <em>jq<\/em> Binary<\/p>\n<p>For 64-bit linux OS: \u00a0wget http:\/\/stedolan.github.io\/jq\/download\/linux64\/jq<\/p>\n<p>For 32-bit linux OS : wget <\/p>\n<p><span style=\"color: #445263\"><span style=\"font-family: Arial, Helvetica, sans-serif\"><strong>Step 2:<\/strong> chmod +x .\/jq <\/span><\/span><\/p>\n<p><span style=\"color: #445263\"><span style=\"font-family: Arial, Helvetica, sans-serif\"><strong>Step 3:<\/strong> sudo cp jq \/usr\/bin<\/span><\/span><\/p>\n<p>In this blog, I will explain things with the help of <em><span style=\"color: #445263\"><span style=\"font-family: Arial, Helvetica, sans-serif\">Demo.json <\/span><\/span><span style=\"color: #445263\"><span style=\"font-family: Arial, Helvetica, sans-serif\">or demo.txt<\/span><\/span><\/em><\/p>\n<p>[js]<br \/>\n{<br \/>\n  &amp;quot;OwnerId&amp;quot;: &amp;quot;xyz&amp;quot;,<br \/>\n  &amp;quot;ReservationId&amp;quot;: &amp;quot;abc&amp;quot;,<br \/>\n  &amp;quot;Groups&amp;quot;: [],<br \/>\n  &amp;quot;State&amp;quot;: {<br \/>\n      &amp;quot;Code&amp;quot;: 0,<br \/>\n      &amp;quot;Name&amp;quot;: &amp;quot;pending&amp;quot;<br \/>\n    },<br \/>\n  &amp;quot;Instances&amp;quot;: [<br \/>\n  {<br \/>\n    &amp;quot;Monitoring&amp;quot;: {<br \/>\n     &amp;quot;State&amp;quot;: &amp;quot;disabled&amp;quot;<br \/>\n    },<br \/>\n    &amp;quot;SecurityGroups&amp;quot;: [<br \/>\n    {<br \/>\n      &amp;quot;GroupName&amp;quot;: &amp;quot;default&amp;quot;,<br \/>\n      &amp;quot;GroupId&amp;quot;: &amp;quot;sg-xxxx&amp;quot;<br \/>\n    }<br \/>\n    ],<br \/>\n    &amp;quot;NetworkInterfaces&amp;quot;: [<br \/>\n     {<br \/>\n        &amp;quot;PrivateIpAddresses&amp;quot;: [<br \/>\n        {<br \/>\n           &amp;quot;PrivateDnsName&amp;quot;: &amp;quot;ip-xx-xx-yy-yyy&amp;quot;,<br \/>\n           &amp;quot;Primary&amp;quot;: true,<br \/>\n           &amp;quot;PrivateIpAddress&amp;quot;: &amp;quot;xx.xx.yy.yyy&amp;quot;<br \/>\n        }<br \/>\n        ],<br \/>\n        &amp;quot;PrivateDnsName&amp;quot;: &amp;quot;ip-xxx-xx-yy-yyy&amp;quot;<br \/>\n       }<br \/>\n     ]<br \/>\n   }<br \/>\n ]<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>Some of the frequently used use-cases have been discussed below with Command and their respective output:<\/p>\n<p><strong>1 : To parse a JSON object:<\/strong><\/p>\n<p>Command :<\/p>\n<p>[js]cat Demo.json | jq &#8216;.OwnerId'[\/js]<\/p>\n<p>Output :<\/p>\n<p>[js]&amp;quot;xyz&amp;quot;[\/js]<\/p>\n<p><strong>2: To parse a nested JSON object:<\/strong><\/p>\n<p>Command :<\/p>\n<p>[js]cat Demo.json | jq &#8216;.State.Name'[\/js]<\/p>\n<p>Output :<\/p>\n<p>[js]&amp;quot;pending&amp;quot;[\/js]<\/p>\n<p><strong>3: To extract specific fields from a JSON object:<\/strong><\/p>\n<p>Command :<\/p>\n<p>[js]cat Demo.json | jq &#8216;.State | { Code , Name}'[\/js]<\/p>\n<p>Output :<\/p>\n<p>[js]{<br \/>\n&amp;quot;Name&amp;quot;: &amp;quot;pending&amp;quot;,<br \/>\n&amp;quot;Code&amp;quot;: 0<br \/>\n}[\/js]<\/p>\n<p><strong>4: To parse a JSON array:<\/strong><\/p>\n<p>Command :<\/p>\n<p>[js]cat Demo.json | jq &#8216;.Instances[0].Monitoring'[\/js]<\/p>\n<p>Output :<\/p>\n<p>[js]{<br \/>\n&amp;quot;State&amp;quot;: &amp;quot;disabled&amp;quot;<br \/>\n}[\/js]<\/p>\n<p>Command :<\/p>\n<p>[js]cat Demo.json | jq &#8216;.Instances[0].Monitoring.State'[\/js]<\/p>\n<p>Output :<\/p>\n<p>[js]&amp;quot;disabled&amp;quot;[\/js]<\/p>\n<p>Command :<\/p>\n<p>[js]cat Demo.json | jq &#8216;.Instances[0].NetworkInterfaces[0].PrivateIpAddresses[0].PrivateDnsName'[\/js]<\/p>\n<p>Output :<\/p>\n<p>[js]&amp;quot;ip-xxx-xx-yy-yyy&amp;quot;[\/js]<\/p>\n<p><em>In cases where you only need the value; you have to remove the \u201c\u201d by using pipeline in linux<\/em><\/p>\n<p>Command :<\/p>\n<p>[js]cat Demo.json | jq &#8216;.OwnerId&#8217; | cut -d &amp;quot;\\&amp;quot;&amp;quot; -f 2[\/js]<\/p>\n<p>Output :<\/p>\n<p>[js]xyz[\/js]<\/p>\n<p>Hope you will be able to parse JSON by command line for your next project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java development is often challenging, especially when you need to parse JSON from the command line. In my recent\u00a0Java development project\u00a0I had\u00a0parse JSON from the command line\u00a0and I explored\u00a0available options one-by-one to find a close fit solution which can be implemented quickly.\u00a0 Requirements To parse json from the command line you&#8217;ll need a software called [&hellip;]<\/p>\n","protected":false},"author":89,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[446,1],"tags":[1249,4263,49,260,1248],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11130"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/89"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11130"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11130\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}