{"id":1538,"date":"2023-03-06T14:17:20","date_gmt":"2023-03-06T13:17:20","guid":{"rendered":"https:\/\/alessandromasciadri.com\/?p=1538"},"modified":"2023-03-06T14:21:13","modified_gmt":"2023-03-06T13:21:13","slug":"come-stampare-con-python-file-json-pretty-print","status":"publish","type":"post","link":"https:\/\/alessandromasciadri.com\/come-stampare-con-python-file-json-pretty-print\/","title":{"rendered":"Come stampare con Python file JSON (Pretty Print)"},"content":{"rendered":"\t\t<div data-akihiro-type=\"ama-post\" data-akihiro-id=\"1538\" class=\"akihiro akihiro-1538\" data-akihiro-post-type=\"post\">\n\t\t\t\t<div class=\"akihiro-element akihiro-element-b21b995 e-flex e-con-boxed e-con e-parent\" data-id=\"b21b995\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"akihiro-element akihiro-element-3f91488 akihiro-widget akihiro-widget-text-editor\" data-id=\"3f91488\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>We can use the Python json module to pretty-print the JSON data. The json module is recommended to work with JSON files. We can use the dumps() method to get the pretty formatted JSON string.<\/p><p>Per stampare in modo umanamente leggibile a console file JSON, possiamo ricorrere all&#8217;omonimo modulo json di Python. Questo modulo \u00e8 caldamente raccomandato quando si lavora con file JSON e anche in questo caso uno dei suoi metodi ci viene in aiuto per risolvere il problema. Vediamo quindi come stampare file JSON con indentazione (Pretty Printing).<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-e0b961e akihiro-widget akihiro-widget-heading\" data-id=\"e0b961e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h3 class=\"akihiro-heading-title akihiro-size-default\">Il metodo dumps()<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-7cd37de akihiro-widget akihiro-widget-text-editor\" data-id=\"7cd37de\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\tGrazie al metodo <code>dumps()<\/code> contenuto nel modulo json di Python, partendo da un JSON file possiamo ottenere una JSON string ben indentata. Vediamo qui di seguito come fare.\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-6d59c86 akihiro-widget akihiro-widget-heading\" data-id=\"6d59c86\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h5 class=\"akihiro-heading-title akihiro-size-default\">Importare il modulo json<\/h5>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-ac41a06 akihiro-widget akihiro-widget-text-editor\" data-id=\"ac41a06\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Come prima cosa importiamo il modulo json:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-b1369d8 akihiro-widget akihiro-widget-code-highlight\" data-id=\"b1369d8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-python \">\n\t\t\t\t<code readonly=\"true\" class=\"language-python\">\n\t\t\t\t\t<xmp>import json<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-0e17d4b akihiro-widget akihiro-widget-heading\" data-id=\"0e17d4b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t<h5 class=\"akihiro-heading-title akihiro-size-default\">Creare una JSON string<\/h5>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-981c4f7 akihiro-widget akihiro-widget-text-editor\" data-id=\"981c4f7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Siamo ora pronti per creare una JSON string partendo da un qualsiasi json file. Utilizziamo le seguenti righe di codice per fare ci\u00f2:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-5c1d303 akihiro-widget akihiro-widget-code-highlight\" data-id=\"5c1d303\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-python \">\n\t\t\t\t<code readonly=\"true\" class=\"language-python\">\n\t\t\t\t\t<xmp>with open('my-file.json', 'r') as openfile:\n    json_object = json.load(openfile)\n    json_pretty_string = json.dumps(json_object, indent=2)\n\nprint(json_pretty_string)<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-8198376 akihiro-widget akihiro-widget-text-editor\" data-id=\"8198376\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t\t\tNell&#8217;esempio qui sopra abbiamo aperto in lettura il file denominato <code>my-file.json<\/code> e da esso generato una JSON string utilizzando il metodo <code>dumps()<\/code>. L&#8217;output che otterremo sar\u00e0 il seguente:\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"akihiro-element akihiro-element-11fb9ee akihiro-widget akihiro-widget-code-highlight\" data-id=\"11fb9ee\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"akihiro-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-okaidia copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-json \">\n\t\t\t\t<code readonly=\"true\" class=\"language-json\">\n\t\t\t\t\t<xmp>[\n  {\n    \"ID\": 10,\n    \"Name\": \"Alessandro\",\n    \"Role\": \"Developer\"\n  },\n  {\n    \"ID\": 20,\n    \"Name\": \"David\",\n    \"Role\": \"Designer\"\n  }\n]<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>We can use the Python json module to pretty-print the JSON data. The json module is recommended to work with JSON files. We can use the dumps() method to get the pretty formatted JSON string. Per stampare in modo umanamente leggibile a console file JSON, possiamo ricorrere all&#8217;omonimo modulo json di Python. Questo modulo \u00e8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[2,12],"class_list":["post-1538","post","type-post","status-publish","format-standard","hentry","category-programmazione","tag-code","tag-python"],"_links":{"self":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/1538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/comments?post=1538"}],"version-history":[{"count":4,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/1538\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/posts\/1538\/revisions\/1542"}],"wp:attachment":[{"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/media?parent=1538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/categories?post=1538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alessandromasciadri.com\/ama-json\/wp\/v2\/tags?post=1538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}