{"id":41125,"date":"2021-08-03T09:30:26","date_gmt":"2021-08-03T07:30:26","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=41125"},"modified":"2023-06-21T16:55:44","modified_gmt":"2023-06-21T14:55:44","slug":"json-file-in-python","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/blog-python\/archivo-json-en-python\/","title":{"rendered":"How to read and write to a JSON file in Python"},"content":{"rendered":"<p>Using JavaScript objects (<strong>JSON<\/strong>) is a powerful tool for quickly exchanging data between various programming platforms. Whether storing data or creating an API, converting your data to JSON makes it reusable, regardless of the technology accessing it.<\/p>\n\n\n\n<p>Below we explain how to read and write to a JSON file in <strong>Python<\/strong>.<\/p>\n\n\n\n<p>To become a highly qualified expert in this field in just 12 months, the <a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/eiposgrados.com\/master-en-python\/\">Master of Advanced Programming in Python for Hacking, Big Data and Machine Learning<\/a> It will train you so that you obtain the necessary skills. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/eiposgrados.com\/eng\/programs\/master-in-python\/?utm_source=entrada-python\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1000\" height=\"400\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python.png\" alt=\"Python Banner\" class=\"wp-image-84816\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python.png 1000w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-300x120.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-768x307.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-123x49.png 123w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-500x200.png 500w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-800x320.png 800w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-200x80.png 200w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Difference between a JSON object and a Python dictionary<\/h2>\n\n\n\n<p>The difference between a JSON object and a Python dictionary is minimal, therefore it is easy to store a Python dictionary as JSON. But to make it work, we need the parser library&nbsp;<strong>json<\/strong>&nbsp;.<\/p>\n\n\n\n<p>We are going to learn basic knowledge of using JSON files in Python. To start, we create a JSON file in the root directory of our project.<\/p>\n\n\n\n<p>We create and open a Python file in the same directory and with this we can write a dictionary in the file <strong>JSON<\/strong> Using our Python code:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/json-en-python-1024x182.png\" alt=\"how to read and write a json file in python\" class=\"wp-image-41127\" width=\"688\" height=\"122\" title=\"\"><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">How do we store a list as JSON in Python?<\/h2>\n\n\n\n<p>Sometimes we may have one or more lists and we want to save them as JSON. A good way to do this is to convert them into dictionaries before writing them to the file. <strong>JSON<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/realizar-json-en-python-1024x279.png\" alt=\"\" class=\"wp-image-41130\" width=\"609\" height=\"165\" title=\"\"><\/figure>\n<\/div>\n\n\n<p>If we want to merge two lists before writing them to the file <strong>JSON<\/strong> We can do it with the use of FOR loops in the following way:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/archivos-json-1024x333.png\" alt=\"\" class=\"wp-image-41128\" width=\"550\" height=\"179\" title=\"\"><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Access JSON data<\/h2>\n\n\n\n<p>Access and consult file data <strong>JSON<\/strong> It is very simple<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/python-archivos-en-json-1024x311.png\" alt=\"\" class=\"wp-image-41129\" width=\"610\" height=\"185\" title=\"\"><\/figure>\n<\/div>\n\n\n<p>And if we want to obtain more specific data we can indicate it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/archivo-json-en-python.png\" alt=\"\" class=\"wp-image-41126\" width=\"452\" height=\"219\" title=\"\"><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Quick queries in Python with JSON<\/h2>\n\n\n\n<p>The objects <strong>JSON<\/strong> In addition to being multiplatform, they are very lightweight and can improve the speed of response to our queries and, as we have seen, review the data in the files. <strong>JSON<\/strong> in Python it&#039;s pretty simple.\u00a0<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/eiposgrados.com\/eng\/programs\/master-in-python\/?utm_source=entrada-python\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"400\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2.png\" alt=\"Python 2 Banner\" class=\"wp-image-84818\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2.png 1000w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2-300x120.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2-768x307.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2-123x49.png 123w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2-500x200.png 500w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2-800x320.png 800w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/08\/banner-python-2-200x80.png 200w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In this post we show you how to read and write a JSON file in Python. We are going to learn basic knowledge of using JSON files in Python. Forward!<\/p>","protected":false},"author":1,"featured_media":41136,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-41125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/41125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=41125"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/41125\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/41136"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=41125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=41125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=41125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}