{"id":301937,"date":"2021-07-15T14:30:00","date_gmt":"2021-07-15T11:30:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-convert-xml-to-json-on-the-command-line-cloudsavvy-it\/"},"modified":"2021-07-15T14:30:00","modified_gmt":"2021-07-15T11:30:00","slug":"how-to-convert-xml-to-json-on-the-command-line-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-convert-xml-to-json-on-the-command-line-cloudsavvy-it\/","title":{"rendered":"#How to Convert XML to JSON on the Command Line \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a303f20d7f02\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a303f20d7f02\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-convert-xml-to-json-on-the-command-line-cloudsavvy-it\/#Use_the_xq_Utility\" >Use the xq Utility<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-convert-xml-to-json-on-the-command-line-cloudsavvy-it\/#One_Problem_With_XML_to_JSON_Conversion\" >One Problem With XML to JSON Conversion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Convert XML to JSON on the Command Line \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage imgchk9 wp-image-5360 size-full\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/cc4b3268.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/cc4b3268.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/cc4b3268.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-vector\/beige-xml-file-document-download-button-1496606852\" data-credittext=\"Shutterstock\/VectorV\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/beige-xml-file-document-download-button-1496606852\">Shutterstock\/VectorV<\/a><\/span><\/figcaption><\/figure>\n<p>XML is complicated, and parsing it natively is fairly hard, even with <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>ing languages. Luckily, there\u2019s a utility that can convert XML to JSON, which is easier to work with, both in scripts and on the command line.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Use_the_xq_Utility\"><\/span>Use the xq Utility<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You\u2019ll want to use a custom made utility for this, rather than trying to parse it with something like regex, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/stackoverflow.com\/questions\/1732348\/regex-match-open-tags-except-xhtml-self-contained-tags\/1732454#1732454\">which is a bad idea<\/a>. There\u2019s a utility called <code>xq<\/code>\u00a0that is perfect for this task. It\u2019s installed alongside <code>yq<\/code>, which works for YAML. You can install <code>yq<\/code>\u00a0from pip:<\/p>\n<pre>pip install yq<\/pre>\n<p>Under the hood, this utility uses <code>jq<\/code>\u00a0to handle working with JSON, so you\u2019ll need to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/stedolan.github.io\/jq\/\">download the binary,<\/a> and move it to somewhere on your PATH (<code>\/usr\/local\/bin\/<\/code>\u00a0should work fine).<\/p>\n<p>Now, you\u2019ll be able to parse XML input by piping it into <code>xq<\/code>:<\/p>\n<pre>cat xml | xq .<\/pre>\n<p>The <code>.<\/code>\u00a0operator means you want to convert all of the XML to JSON. You can actually use full <code>jq<\/code>\u00a0syntax here to select sub-elements, which you can read our guide on.<\/p>\n<p>You can also output <code>xq<\/code>\u2018s response as XML with the <code>-x<\/code>\u00a0flag:<\/p>\n<pre>xq -x<\/pre>\n<p>This lets you use <code>jq<\/code>\u2018s selection syntax to parse XML while keeping it in XML form. Though it doesn\u2019t seem to be able to convert the other way, as <code>xq<\/code>\u00a0still wants XML syntax in.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"One_Problem_With_XML_to_JSON_Conversion\"><\/span>One Problem With XML to JSON Conversion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>XML to JSON isn\u2019t a perfect conversion\u2014in XML, the order of the elements can matter, and keys can be duplicated. A document like:<\/p>\n<pre>&lt;e&gt;&#13;\n  &lt;a&gt;some&lt;\/a&gt;&#13;\n  &lt;b&gt;textual&lt;\/b&gt;&#13;\n  &lt;a&gt;content&lt;\/a&gt;&#13;\n&lt;\/e&gt;<\/pre>\n<p>Would produce an error if translated outright to JSON, because the <code>a<\/code>\u00a0key exists twice. So, it\u2019s converted to an array, which does break the order. <code>xq<\/code>\u00a0returns the following output for that bit of XML:<\/p>\n<pre>{&#13;\n  \"e\": {&#13;\n    \"a\": [&#13;\n      \"some\",&#13;\n      \"content\"&#13;\n    ],&#13;\n    \"b\": \"textual\"&#13;\n  }&#13;\n}<\/pre>\n<p>Which is technically correct, just not ideal in all situations. You\u2019ll want to double-check and make sure your XML conversion has no issues.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/1889\/how-to-convert-xml-to-json-on-the-command-line\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Convert XML to JSON on the Command Line \u2013 CloudSavvy IT&#8221; Shutterstock\/VectorV XML is complicated, and parsing it natively is fairly hard, even with scripting languages. Luckily, there\u2019s a utility that can convert XML to JSON, which is easier to work with, both in scripts and on the command line. Use the xq&#8230;<\/p>\n","protected":false},"author":1,"featured_media":301938,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/cc4b3268.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-301937","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/301937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=301937"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/301937\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/301938"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=301937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=301937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=301937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}