{"id":4001,"date":"2023-12-01T14:35:17","date_gmt":"2023-12-01T14:35:17","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=4001"},"modified":"2023-12-01T14:41:25","modified_gmt":"2023-12-01T14:41:25","slug":"python-read-json-from-a-file-into-dictionary","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=4001","title":{"rendered":"Python: Read JSON From a File into Dictionary"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Read in JSON from a file and convert into a Dictionary<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n\nimport json\n\nf = open('data.json')\r\ndata = json.load(f)\r\ndata = json.dumps(data,indent=2)\r\n\n# Prints the Imported JSON\nprint(data)\n\n# Loads JSON into a Dictionary\nobjArrayInfoDict = json.loads(data)\r\nprint(objArrayInfoDict)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example I was testing getting data from the Slack service status API, so wanted to simulate broken-ness to test my script was working correctly. I did this by creating a file with a sample set of JSON that showed a problem, as I could easily get JSON of when it was working, which was the case as I was writing the script!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: data.json file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>  {\r\n    \"status\": \"active\",\r\n    \"date_created\": \"2019-04-09T07:35:46-07:00\",\r\n    \"date_updated\": \"2019-04-09T07:35:46-07:00\",\r\n    \"active_incidents\": &#91;\r\n      {\r\n        \"id\": \"1\",\r\n        \"date_created\": \"2018-09-07T14:35:00-07:00\",\r\n        \"date_updated\": \"2018-09-07T18:34:15-07:00\",\r\n        \"title\": \"One thing is broken\",\r\n        \"type\": \"incident\",\r\n        \"status\": \"active\",\r\n        \"url\": \"https:\/\/status.domain.com\/issue1\/\",\r\n        \"services\": &#91;\r\n          \"Apps\/Integrations\/APIs\"\r\n        ],\r\n        \"notes\": &#91;\r\n          {\r\n            \"date_created\": \"2018-09-07T18:34:15-07:00\",\r\n            \"body\": \"Its all gone wrong!\"\r\n          }\r\n        ]\r\n      },\r\n      {\r\n        \"id\": \"2\",\r\n        \"date_created\": \"2018-09-07T14:35:00-07:00\",\r\n        \"date_updated\": \"2018-09-07T18:34:15-07:00\",\r\n        \"title\": \"Its all gone wrong!\",\r\n        \"type\": \"incident\",\r\n        \"status\": \"active\",\r\n        \"url\": \"https:\/\/status.domain.com\/issue2\/\",\r\n        \"services\": &#91;\r\n          \"Apps\/Integrations\/APIs\"\r\n        ],\r\n        \"notes\": &#91;\r\n          {\r\n            \"date_created\": \"2018-09-07T18:34:15-07:00\",\r\n            \"body\": \"Its all on fire!\"\r\n          }\r\n        ]\r\n      }\r\n    ]\r\n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Read in JSON from a file and convert into a Dictionary For example I was testing getting data from the Slack service status API, so wanted to simulate broken-ness to test my script was working correctly. I did this by creating a file with a sample set of JSON that showed a problem, as I &#8230; <a title=\"Python: Read JSON From a File into Dictionary\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=4001\" aria-label=\"Read more about Python: Read JSON From a File into Dictionary\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,20],"tags":[],"class_list":["post-4001","post","type-post","status-publish","format-standard","hentry","category-python","category-random"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4001"}],"version-history":[{"count":2,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4001\/revisions"}],"predecessor-version":[{"id":4004,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4001\/revisions\/4004"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}