{"id":9438,"date":"2020-04-26T00:42:57","date_gmt":"2020-04-25T19:12:57","guid":{"rendered":"https:\/\/java2blog.com\/?p=9438"},"modified":"2023-11-25T10:41:56","modified_gmt":"2023-11-25T05:11:56","slug":"python-pretty-print-json","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-pretty-print-json\/","title":{"rendered":"Python pretty print json"},"content":{"rendered":"<p>In this post, we will see how to pretty print JSON in Python.<\/p>\n<p>We can use the Python JSON module to pretty-print JSON data. We can use json&#8217;s <code>dumps()<\/code> method with <code>indent<\/code> parameter to pretty print json data.<br \/>\n[toc]<\/p>\n<h2>Python pretty print json string<\/h2>\n<p>Let&#8217;s understand this with the help of example.<\/p>\n<pre class=\"lang:python decode:1 \">\nimport json\n\njson_countries = '[{\"ID\":101,\"Name\":\"India\",\"Population\":\"20000\"},' \\\n            '{\"ID\":201,\"Name\":\"Bhutan\",\"Population\":\"3000\"}]'\n\njson_obj_countries = json.loads(json_countries)\n\njson_pretty_str = json.dumps(json_obj_countries, indent=2)\n\nprint(json_pretty_str)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-gray\">\n[<br \/>\n  {<br \/>\n    &#8220;ID&#8221;: 101,<br \/>\n    &#8220;Name&#8221;: &#8220;India&#8221;,<br \/>\n    &#8220;Population&#8221;: &#8220;20000&#8221;<br \/>\n  },<br \/>\n  {<br \/>\n    &#8220;ID&#8221;: 201,<br \/>\n    &#8220;Name&#8221;: &#8220;Bhutan&#8221;,<br \/>\n    &#8220;Population&#8221;: &#8220;3000&#8221;<br \/>\n  }<br \/>\n]\n<\/div>\n<p><strong>Explanation<\/strong><\/p>\n<ol>\n<li>Import json module<\/li>\n<li>Use json&#8217;s <code>loads()<\/code> method to convert json string <code>json_countries<\/code> to json object <code>json_obj_countries<\/code><\/li>\n<li>Use json&#8217;s <code>dumps()<\/code> method to convert json object <code>json_obj_countries<\/code> to pretty print json data string <code>json_pretty_str<\/code><\/li>\n<li>Print the string <code>json_pretty_str<\/code><\/li>\n<\/ol>\n<h2>Python pretty print json File<\/h2>\n<p>We can first <a href=\"https:\/\/java2blog.com\/python-read-file-into-string\/\" rel=\"noopener noreferrer\" target=\"_blank\">read json file into string<\/a> with <code>open()<\/code> function<br \/>\nLet&#8217;s understand this with the help of example.<br \/>\nHere is json file which we are going to read.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2020\/04\/EmployeesJsonFile.png\" alt=\"EmployeesJsonFile\" width=\"411\" height=\"334\" class=\"aligncenter size-full wp-image-9446\" srcset=\"https:\/\/java2blog.com\/wp-content\/uploads\/2020\/04\/EmployeesJsonFile.png 411w, https:\/\/java2blog.com\/wp-content\/uploads\/2020\/04\/EmployeesJsonFile-300x244.png 300w\" sizes=\"(max-width: 411px) 100vw, 411px\" \/><\/p>\n<pre class=\"lang:python decode:1 \">\nimport json\n\nwith open('Employees.json', 'r') as json_file:\n    json_obj_countries = json.load(json_file)\n\nprint(json_obj_countries)\nprint(\"========================================\")\njson_pretty_str = json.dumps(json_obj_countries, indent=2)\nprint(json_pretty_str)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-gray\">\n[{&#8216;Employee ID&#8217;: 101, &#8216;Employee Name&#8217;: &#8216;Arpit&#8217;, &#8216;Age&#8217;: &#8217;24&#8217;}, {&#8216;Employee ID&#8217;: 202, &#8216;Employee Name&#8217;: &#8216;John&#8217;, &#8216;Age&#8217;: &#8217;30&#8217;}]<br \/>\n========================================<br \/>\n[<br \/>\n  {<br \/>\n    &#8220;Employee ID&#8221;: 101,<br \/>\n    &#8220;Employee Name&#8221;: &#8220;Arpit&#8221;,<br \/>\n    &#8220;Age&#8221;: &#8220;24&#8221;<br \/>\n  },<br \/>\n  {<br \/>\n    &#8220;Employee ID&#8221;: 202,<br \/>\n    &#8220;Employee Name&#8221;: &#8220;John&#8221;,<br \/>\n    &#8220;Age&#8221;: &#8220;30&#8221;<br \/>\n  }<br \/>\n]\n<\/div>\n<p><strong>Explanation<\/strong><\/p>\n<ol>\n<li>Import json module<\/li>\n<li>Use open(&#8216;Employees.json&#8217;, &#8216;r&#8217;) function to read file into <code>json_file<\/code><\/li>\n<li>Use json&#8217;s <code>load()<\/code> method to convert file object <code>json_file<\/code> to json object <code>json_obj_countries<\/code><\/li>\n<li>Use json&#8217;s <code>dumps()<\/code> method to convert json object <code>json_obj_countries<\/code> to pretty print json data string <code>json_pretty_str<\/code><\/li>\n<li>Print the string <code>json_pretty_str<\/code><\/li>\n<\/ol>\n<p>You can use <code>indent=$space<\/code> with dumps to pretty print json string.<\/p>\n<p>For example:<br \/>\n<code>json_pretty_str = json.dumps(json_obj_countries, indent=2)<\/code> will output as:<\/p>\n<pre>\n[\n  {\n    \"Employee ID\": 101,\n    \"Employee Name\": \"Arpit\",\n    \"Age\": \"24\"\n  },\n  {\n    \"Employee ID\": 202,\n    \"Employee Name\": \"John\",\n    \"Age\": \"30\"\n  }\n]\n<\/pre>\n<p>whereas, <code>json_pretty_str = json.dumps(json_obj_countries, indent=4)<\/code> will output as:<\/p>\n<pre>\n[\n    {\n        \"Employee ID\": 101,\n        \"Employee Name\": \"Arpit\",\n        \"Age\": \"24\"\n    },\n    {\n        \"Employee ID\": 202,\n        \"Employee Name\": \"John\",\n        \"Age\": \"30\"\n    }\n]\n<\/pre>\n<p>That&#8217;s all about python pretty print json.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will see how to pretty print JSON in Python. We can use the Python JSON module to pretty-print JSON data. We can use json&#8217;s dumps() method with indent parameter to pretty print json data. [toc] Python pretty print json string Let&#8217;s understand this with the help of example. import json json_countries [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9438"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=9438"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/9438\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=9438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=9438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=9438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}