{"id":57,"date":"2015-07-02T21:16:10","date_gmt":"2015-07-03T05:16:10","guid":{"rendered":"http:\/\/www.jwdev.com\/?p=57"},"modified":"2016-10-10T18:40:33","modified_gmt":"2016-10-11T02:40:33","slug":"inline-parsing-json-objects-in-3-languages","status":"publish","type":"post","link":"https:\/\/www.jwdev.com\/?p=57","title":{"rendered":"Inline parsing JSON objects in 3 languages"},"content":{"rendered":"<p>JavaScript Object Notation commonly known as JSON, is a convenient format for many reasons and it is no wonder that many APIs and web services today support returning (in some cases exclusively) JSON.\u00a0 Many times I have had a need to parse values from large JSON Objects in a shell script environment or a command line shell, and use them as inputs to another program.\u00a0 This has put me on the look out for easy ways to handle inline parsing of JSON Objects using languages commonly available on modern Linux systems.<\/p>\n<p>Below are examples in three languages (Node JS, Python and Perl) to accomplish this task.<\/p>\n<p><strong>Example JSON Object:<\/strong><\/p>\n<pre class=\"brush:js\">{\"product\": {\"builds\": {\"1234\": \"IT WORKS!\"}, \"default\": \"1234\"}}\n<\/pre>\n<p><strong>Inline Node JS:<\/strong><\/p>\n<pre class=\"brush:shell\">node -e \"s=''; i=process.stdin; i.on('data', function(d) { s += d }); i.on('end', function() { j=JSON.parse(s).product; console.log(j.builds[j.default]) })\"\n<\/pre>\n<p><strong>Inline Python:<\/strong><\/p>\n<pre class=\"brush:shell\">python -c 'import sys, json; j=json.load(sys.stdin)[\"product\"]; print j[\"builds\"][j[\"default\"]]'\n<\/pre>\n<p><strong>Inline Perl:<\/strong><\/p>\n<pre class=\"brush:shell\">perl -e 'use JSON; local $\/; $d=decode_json(&lt;&gt;)-&gt;{product}; print $d-&gt;{builds}-&gt;{$d-&gt;{default}}'\n<\/pre>\n<ul>\n<li>The JSON module is required in the above example. If not already available it can easily be installed via CPAN with:<\/li>\n<\/ul>\n<pre class=\"brush:shell\">sudo perl -MCPAN -e 'install JSON'\n<\/pre>\n<p><strong><br \/>\nFull example for Python:<\/strong><\/p>\n<pre>echo '{\"product\": {\"builds\": {\"1234\": \"IT WORKS!\"}, \"default\": \"1234\"}}' |python -c 'import sys, json; j=json.load(sys.stdin)[\"product\"]; print j[\"builds\"][j[\"default\"]]'\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Object Notation commonly known as JSON, is a convenient format for many reasons and it is no wonder that many APIs and web services today support returning (in some cases exclusively) JSON.\u00a0 Many times I have had a need to parse values from large JSON Objects in a shell script environment or a command [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21,10,9],"tags":[18,17,15,14,11,13,12,16],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-devel","category-devops","category-software","category-sysadmin","tag-bash","tag-cli","tag-javascript","tag-json","tag-node","tag-perl","tag-python","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.jwdev.com\/index.php?rest_route=\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jwdev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jwdev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jwdev.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jwdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":15,"href":"https:\/\/www.jwdev.com\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/www.jwdev.com\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/www.jwdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jwdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jwdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}