{"id":129386,"date":"2022-11-01T10:49:11","date_gmt":"2022-11-01T10:49:11","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=852705"},"modified":"2022-11-01T10:49:11","modified_gmt":"2022-11-01T10:49:11","slug":"python-create-json-file","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/11\/01\/python-create-json-file\/","title":{"rendered":"Python Create JSON File"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;852705&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p>This article focuses on working with a JSON file. <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/parse-json-data-in-python\/\" data-type=\"post\" data-id=\"197286\" target=\"_blank\">JSON<\/a> is an acronym for JavaScript Object Notation. This is a flat-text file formatted based on JavaScript (JS) Syntax. <\/p>\n<p>This file is most commonly noted for its ability to transmit data to\/from web applications, such as sending\/receiving data from a Server\/Client to display or retrieve said data.<\/p>\n<p>The JSON file structure has keys and values similar to a Python <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a>.<\/p>\n<p>This structure can contain strings, boolean, integers, floats, <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\" target=\"_blank\" rel=\"noreferrer noopener\">lists<\/a>, and much more. This file structure can be as simple or complex as needed.<\/p>\n<p>This article works with Rivers Clothing, a new start-up. They have hired their first three (3) employees. This employee data is displayed as a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" target=\"_blank\">List of Dictionaries<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">emps = {\"hires\": [{\"ID\": 324, \"name\": \"Alex Smith\", \"title\": \"Manager\"}, {\"ID\": 325, \"name\": \"Micah Jones\", \"title\": \"Designer\"}, {\"ID\": 326, \"name\": \"Sam Quinn\", \"title\": \"Coder\"}]}<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio\" \/>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would we write code to create and work with a JSON File<\/em><em>?<\/em><\/p>\n<ol type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<li>Python Create JSON File if Not Exists<\/li>\n<li>Python Create JSON String from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" target=\"_blank\">List of Dictionaries<\/a><\/li>\n<li>Python Create JSON File and Write Data<\/li>\n<li>Python Read JSON File<\/li>\n<li>Python Access JSON Elements<\/li>\n<li>Python Delete JSON Elements<\/li>\n<\/ol>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Python Create JSON File if Not Exists<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example creates an empty JSON file if one does not exist in the current working directory.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4, 6-10\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os filename = 'employees.json'\nisFile = os.path.isfile(filename) if (not isFile): with open(filename, 'w') as fp: pass\nelse: print(f'The {filename} file exists.')<\/pre>\n<p>The first line in the above code snippet imports Python&#8217;s built-in <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/exploring-pythons-os-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/exploring-pythons-os-module\/\" target=\"_blank\"><code>os<\/code><\/a> library. This allows access to and manipulation of files and folders. <\/p>\n<p>The following line declares the JSON file name, <code>employees.json<\/code> and saves it to the variable <code>filename<\/code>.<\/p>\n<p>The next line calls the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/exploring-pythons-os-module\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/exploring-pythons-os-module\/\" target=\"_blank\"><code>os.path.isfile()<\/code><\/a> function and passes it one (1) argument, <code>filename<\/code>. This function checks the current directory for the existence of the <code>employees.json<\/code> file<\/p>\n<p>The first time this code runs, and this file is <strong>not <\/strong>found (<code>isFile <\/code>is False), an empty file is created and placed into the current working directory. <\/p>\n<p class=\"has-global-color-8-background-color has-background\"><strong><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>Note<\/strong>: The <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-pass-statement\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-pass-statement\/\" target=\"_blank\">pass<\/a><\/code> statement is a placeholder code and does nothing when executed. This is used here so a file is created, and nothing else occurs.<\/p>\n<p>If this code is rerun or the file exists, the following message is output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>The <code>employees.json<\/code> file exists.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>To create this file in a sub-folder, you would modify the code as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"3\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os filename = 'files\\\\employees.json'\nisFile = os.path.isfile(filename) if (not isFile): with open(filename, 'w') as fp: pass\nelse: print(f'The {filename} file exists.')<\/pre>\n<p>When this code runs, the files folder is checked for the existence of the <code><code>employees<\/code>.json<\/code> file.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-create-and-work-with-a-json-file-in-python\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FPcrUJjfmOwE%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Python Create JSON String from List of Dictionaries<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example creates a JSON string from a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" target=\"_blank\">List of Dictionaries<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1, 13\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import json emps = {\"hires\" :[{\"empID\": \"RC-3243\", \"name\": \"Alexa Smith\", \"title\": \"Manager\"}, {\"empID\": \"RC-3244\", \"name\": \"Micah Jones\", \"title\": \"Designer\"}, {\"empID\": \"RC-3245\", \"name\": \"Sam Quinn\", \"title\": \"Coder\"}]} json_str = json.dumps(emps, indent=4)\nprint(json_str)<\/pre>\n<p>The above code snippet imports the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/json.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/json.html\" target=\"_blank\"><code>json<\/code><\/a> library. This library allows access to JSON functions. <\/p>\n<p>The following three (3) lines construct a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" target=\"_blank\">List of Dictionaries<\/a> containing data for the new hires. The results save to <code>emps<\/code>.<\/p>\n<p>Next, <code><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/json.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/json.html\" target=\"_blank\">json.dumps()<\/a> <\/code>is called and passed two (2) arguments: a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" target=\"_blank\">List of Dictionaries<\/a>, <code>emps<\/code>, and for formatting, spaces to indent. The results save to <code>json_str<\/code> and output to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>{<br \/>\"hires\": [<br \/> {<br \/> \"ID\": 3243,<br \/> \"name\": \"Alexa Smith\",<br \/> \"title\": \"Manager\"<br \/> },<br \/> {<br \/> \"ID\": 3244,<br \/> \"name\": \"Micah Jones\",<br \/> \"title\": \"Designer\"<br \/> },<br \/> {<br \/> \"ID\": 3245,<br \/> \"name\": \"Sam Quinn\",<br \/> \"title\": \"Coder\"<br \/> } <br \/> ]<br \/>}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note<\/strong>: The <code><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/json.html\" target=\"_blank\">json.dumps()<\/a> <\/code>function formats the JSON string. To write this to a file, <code><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/json.html\" target=\"_blank\">json.dump()<\/a><\/code> is used.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Python Create JSON File and Write Data<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example writes a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" target=\"_blank\">List of Dictionaries<\/a> to a JSON file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"12-13\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport json filename = 'employees.json'\nisFile = os.path.isfile(filename) emps = {\"hires\" :[{\"ID\": 3243, \"name\": \"Alexa Smith\", \"title\": \"Manager\"}, {\"ID\": 3244, \"name\": \"Micah Jones\", \"title\": \"Designer\"}, {\"ID\": 3245, \"name\": \"Sam Quinn\", \"title\": \"Coder\"}]} if (not isFile): with open(filename, 'w') as fp: json.dump(emps, fp, indent=4)\nelse: print(f'An error occurred writing to {filename}.') <\/pre>\n<p>The above code snippet adds the two (2) highlighted lines to write the formatted JSON string to the <code>employees.json<\/code> file. <\/p>\n<p>The contents of this file is as follows:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>{<br \/>\"hires\": [<br \/> {<br \/> \"ID\": 3243,<br \/> \"name\": \"Alexa Smith\",<br \/> \"title\": \"Manager\"<br \/> },<br \/> {<br \/> \"ID\": 3244,<br \/> \"name\": \"Micah Jones\",<br \/> \"title\": \"Designer\"<br \/> },<br \/> {<br \/> \"ID\": 3245,<br \/> \"name\": \"Sam Quinn\",<br \/> \"title\": \"Coder\"<br \/> } <br \/> ]<br \/>}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2> Python Read JSON File<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">In this example, the JSON file saved earlier is read back in.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5,7-10\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport json filename = 'employees.json'\njson_str = '' with open(filename , 'r') as fp: for l in fp: l.replace('\\n', '') json_str += l print(json_str)<\/pre>\n<p>This code snippet opens, reads the <code>employees.json<\/code> file and saves it to <code>json_str<\/code>. Upon each iteration, any additional newline (<code>\\n<\/code>) characters are removed using the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-string-replace-2\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-string-replace-2\/\" target=\"_blank\">replace()<\/a> <\/code>function. <\/p>\n<p>Once all lines have been read, the contents of <code>json_str<\/code> is output to the terminal and is the same as indicated above.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-create-and-work-with-a-json-file-in-python\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-UkcLQxzPA4%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Python Access JSON Elements<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example reads in the previously saved JSON file and accesses the ID element from each hire.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"12-14\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport json filename = 'employees.json'\njson_str = '' with open(filename , 'r') as fp: for l in fp: l.replace('\\n', '') json_str += l all_emps = json.loads(json_str)\nalexa_id = all_emps['hires'][0]['ID']\nsam_id = all_emps['hires'][2]['ID']\nprint(alexa_id, sam_id)<\/pre>\n<p>As outlined earlier in this article, the <code>employees.json<\/code> file is read in, parsed and saved to <code>json_str<\/code>.<\/p>\n<p>Then, <code>json_str <\/code>is loaded using <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/json.html\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/json.html\" target=\"_blank\"><code>json_loads()<\/code><\/a> and passed one (1) argument, the <code>json_str<\/code> created above. This gives us access to the elements.<\/p>\n<p>The following lines access the Employee IDs for Alexa and Micah and outputs same to the terminal.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code> 3243 3245<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>To iterate and display the <code>ID<\/code> and <code>name<\/code> for each new <code>hire<\/code>, run the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"14-15\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport json filename = 'employees.json'\njson_str = '' with open(filename , 'r') as fp: for l in fp: l.replace('\\n', '') json_str += l all_emps = json.loads(json_str) for item in all_emps['hires']: print(item['ID'], item['name'])<\/pre>\n<p>Another option is to use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a> to retrieve the data:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">results = [item['ID'] for item in all_emps['hires']]\nprint(results)<\/pre>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/how-to-create-and-work-with-a-json-file-in-python\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F9qsq2Vf48W8%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Python Delete Elements<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example reads in the previously saved JSON file and deletes the new hire, Micah Jones.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"12-13,,15-18\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport json filename = 'employees.json'\njson_str = '' with open(filename , 'r') as fp: for l in fp: l.replace('\\n', '') json_str += l all_emps = json.loads(json_str)\nidx = 0 for item in all_emps['hires']: if idx == 1: del all_emps['hires'][idx] idx += 1\nprint(all_emps)<\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Summary<\/h2>\n<p>We hope you enjoyed this article about converting a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" target=\"_blank\">List of Dictionaries<\/a> to JSON and everything in between!<\/p>\n<p>Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2>Programmer Humor &#8211; Blockchain<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"394\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31.png\" alt=\"\" class=\"wp-image-457795\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31.png 280w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-31-213x300.png 213w\" sizes=\"auto, (max-width: 280px) 100vw, 280px\" \/><figcaption><em>&#8220;Blockchains are like grappling hooks, in that it&#8217;s extremely cool when you encounter a problem for which they&#8217;re the right solution, but it happens way too rarely in real life.&#8221;<\/em> <strong>source <\/strong> &#8211; <a href=\"https:\/\/imgs.xkcd.com\/comics\/blockchain.png\" data-type=\"URL\" data-id=\"https:\/\/imgs.xkcd.com\/comics\/blockchain.png\" target=\"_blank\" rel=\"noreferrer noopener\">xkcd<\/a><\/figcaption><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation and Solution Overview This article focuses on working with a JSON file. JSON is an acronym for JavaScript Object Notation. This is a flat-text file formatted based on JavaScript (JS) Syntax. This file is most commonly noted for its ability to transmit data to\/from web applications, such as sending\/receiving [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-129386","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=129386"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129386\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}