{"id":121433,"date":"2026-07-08T22:35:38","date_gmt":"2026-07-08T17:05:38","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=121433"},"modified":"2026-07-08T22:35:40","modified_gmt":"2026-07-08T17:05:40","slug":"how-to-read-json-file-in-python","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-read-json-file-in-python\/","title":{"rendered":"How to Read JSON File in Python: A Complete Guide"},"content":{"rendered":"\n<p>Many Python developers working with APIs, configuration files, and data pipelines encounter JSON as the most common data exchange format. Knowing how to read JSON file in Python correctly, access nested values, handle errors, and work with large files is a foundational skill for backend development, data engineering, and automation. This guide walks through every practical scenario you will encounter when working with JSON in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>Reading a JSON file in Python is done using the built-in json module with the json.load() function to read from a file or json.loads() to parse a JSON string. <\/li>\n\n\n\n<li>Python converts JSON data automatically into native Python objects: objects become dictionaries, arrays become lists, strings remain strings, and numbers remain integers or floats. <\/li>\n\n\n\n<li>This guide covers every method to read, access, and handle JSON files in Python with practical code examples.<\/li>\n<\/ul>\n\n\n\n<p>Want to build strong Python data handling skills with real-world projects and structured guidance? Explore <strong>HCL GUVI&#8217;s <\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=read-json-file-in-python\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Python Programming Course<\/strong><\/a>, designed for beginners and professionals who want to apply Python confidently in production.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is JSON and Why Is It Used?<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/complete-guide-on-how-to-open-a-json-file\/\" target=\"_blank\" rel=\"noreferrer noopener\">JSON (JavaScript Object Notation) <\/a>is a lightweight, human-readable format for storing and exchanging structured data. It is the standard format used by <a href=\"https:\/\/www.guvi.in\/blog\/what-is-rest-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">REST APIs<\/a>, configuration files, and databases like <a href=\"https:\/\/www.guvi.in\/blog\/what-is-mongo-db\/\" target=\"_blank\" rel=\"noreferrer noopener\">MongoDB<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A JSON file looks like this:\n\n{\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"name\": \"Priya\",\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"age\": 28,\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"skills\": &#91;\"Python\", \"SQL\", \"AWS\"],\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"address\": {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"city\": \"Chennai\",\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"state\": \"Tamil Nadu\"\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>JSON supports six data types: strings, numbers, booleans, null, arrays, and objects. <a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> maps each of these directly to native types when loading.<\/p>\n\n\n\n<p><strong>Read More: <\/strong><a href=\"https:\/\/www.guvi.in\/blog\/file-handling-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>File Handling in Python: A Beginner\u2019s Simple Guide<\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JSON to Python Type Mapping<\/strong><\/h2>\n\n\n\n<p>When you load a JSON file in Python, the json module converts each JSON type automatically:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>JSON Type<\/strong><\/td><td><strong>Python Type<\/strong><\/td><\/tr><tr><td>object<\/td><td>dict<\/td><\/tr><tr><td>array<\/td><td>list<\/td><\/tr><tr><td>string<\/td><td>str<\/td><\/tr><tr><td>number (int)<\/td><td>int<\/td><\/tr><tr><td>number (float)<\/td><td>float<\/td><\/tr><tr><td>true \/ false<\/td><td>True \/ False<\/td><\/tr><tr><td>null<\/td><td>None<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Understanding this mapping helps you access and manipulate JSON data correctly after loading it.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n\n  <strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong>\n  <br \/><br \/>\n\n  Python&#8217;s built-in <strong style=\"color: #FFFFFF;\">json<\/strong> module includes a <strong style=\"color: #FFFFFF;\">C accelerator<\/strong> called <strong style=\"color: #FFFFFF;\">_json<\/strong> that is automatically used when available. This optimized implementation can dramatically speed up JSON parsing and serialization compared to the pure Python fallback, especially for large files and high-volume data processing. As a result, Python&#8217;s standard <strong style=\"color: #FFFFFF;\">json<\/strong> module delivers excellent real-world performance without requiring developers to install additional JSON libraries in most applications.\n\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Read a JSON File in Python<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Method 1: Using json.load() to Read from a File<\/strong><\/h3>\n\n\n\n<p>json.load() reads directly from a file object. This is the standard method for reading JSON from a file on disk.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as file:\n\n&nbsp;&nbsp;&nbsp;&nbsp;data = json.load(file)\n\nprint(data)\n\nprint(type(data))&nbsp; # Output: &lt;class 'dict'&gt;<\/code><\/pre>\n\n\n\n<p>Always use encoding=&#8221;utf-8&#8243; to handle special characters correctly. The with statement ensures the file closes automatically after reading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Method 2: Using json.loads() to Parse a JSON String<\/strong><\/h3>\n\n\n\n<p>json.loads() parses a JSON-formatted string rather than a file. This is the method to use when you receive JSON data from an API response or a variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\njson_string = '{\"name\": \"Arun\", \"age\": 25, \"city\": \"Bangalore\"}'\n\ndata = json.loads(json_string)\n\nprint(data&#91;\"name\"]) &nbsp; # Output: Arun\n\nprint(data&#91;\"age\"])&nbsp; &nbsp; # Output: 25<\/code><\/pre>\n\n\n\n<p>Note the difference: json.load() takes a file object. json.loads() takes a string. The s in loads stands for string.<\/p>\n\n\n\n<p>Want to build strong Python data handling skills with real-world projects and structured guidance? Explore <strong>HCL GUVI&#8217;s <\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=read-json-file-in-python\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Python Programming Course<\/strong><\/a>, designed for beginners and professionals who want to apply Python confidently in production.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Access JSON Data in Python<\/strong><\/h2>\n\n\n\n<p>Once loaded, JSON data becomes a standard Python dictionary. Access values using keys and indices.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nwith open(\"employee.json\", \"r\") as file:\n\n&nbsp;&nbsp;&nbsp;&nbsp;employee = json.load(file)\n\n# Access top-level keys\n\nprint(employee&#91;\"name\"]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Output: Priya\n\nprint(employee&#91;\"age\"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Output: 28\n\n# Access a list value\n\nprint(employee&#91;\"skills\"]&#91;0])&nbsp; &nbsp; &nbsp; # Output: Python\n\n# Access nested object\n\nprint(employee&#91;\"address\"]&#91;\"city\"])&nbsp; # Output: Chennai<\/code><\/pre>\n\n\n\n<p>Use square bracket notation for direct access. Use the .get() method to safely access keys that may not exist:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>city = employee.get(\"address\", {}).get(\"city\", \"Unknown\")\n\nprint(city)&nbsp; # Output: Chennai<\/code><\/pre>\n\n\n\n<p>.get() returns None by default instead of raising a KeyError when the key is missing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reading a JSON File with Multiple Records<\/strong><\/h2>\n\n\n\n<p>JSON files often contain a list of objects rather than a single object. Iterate over the list after loading.<\/p>\n\n\n\n<p>Sample JSON file (employees.json):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n\n&nbsp;&nbsp;&nbsp;&nbsp;{\"name\": \"Arun\", \"department\": \"Engineering\"},\n\n&nbsp;&nbsp;&nbsp;&nbsp;{\"name\": \"Priya\", \"department\": \"Data Science\"},\n\n&nbsp;&nbsp;&nbsp;&nbsp;{\"name\": \"Rahul\", \"department\": \"DevOps\"}\n\n]<\/code><\/pre>\n\n\n\n<p>Reading and iterating:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nwith open(\"employees.json\", \"r\") as file:\n\n&nbsp;&nbsp;&nbsp;&nbsp;employees = json.load(file)\n\nfor employee in employees:\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(f\"{employee&#91;'name']} - {employee&#91;'department']}\")\n\n# Output:\n\n# Arun - Engineering\n\n# Priya - Data Science\n\n# Rahul - DevOps<\/code><\/pre>\n\n\n\n<p>The json.load() call returns a Python list here because the top-level JSON structure is an array.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n\n  <strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong>\n  <br \/><br \/>\n\n  <strong style=\"color: #FFFFFF;\">JSON (JavaScript Object Notation)<\/strong> was popularized by <strong style=\"color: #FFFFFF;\">Douglas Crockford<\/strong> in the early 2000s as a lightweight alternative to XML for exchanging data between applications. Today, JSON is supported natively by virtually every major programming language and has become the de facto standard format for <strong style=\"color: #FFFFFF;\">REST APIs<\/strong> and web services. Python has included the built-in <strong style=\"color: #FFFFFF;\">json<\/strong> module since <strong style=\"color: #FFFFFF;\">Python 2.6<\/strong>, making it easy to parse, generate, and manipulate JSON data without installing any additional libraries.\n\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Reading JSON files in Python is a foundational skill that appears in almost every real-world Python project, from loading API responses and configuration files to processing large data pipelines. The json module covers the majority of use cases cleanly with just two functions: json.load() for files and json.loads() for strings.<\/p>\n\n\n\n<p>As your projects grow, remember to handle encoding correctly, use .get() for safe key access, validate API responses before parsing, and switch to ijson for large files..<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQ<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1783391939697\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I read a JSON file in Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the json module with json.load() to read from a file object opened in read mode, or json.loads() to parse a JSON-formatted string.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783391944726\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>What is the difference between json.load() and json.loads()?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>json.load() reads from a file object. json.loads() parses a JSON string. The s in loads stands for string.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783391954445\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I access nested values in a JSON file in Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Chain dictionary keys and list indices: data[&#8220;address&#8221;][&#8220;city&#8221;] accesses a nested key. Use .get() to safely access keys that may not exist without raising a KeyError.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783391966994\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I read JSON data from an API in Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the requests library and call response.json() on a successful response. This internally parses the JSON response body and returns a Python dictionary or list.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783391975689\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>What happens if the JSON file has invalid formatting?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Python raises a json.JSONDecodeError. Always wrap json.load() calls in a try-except block to handle malformed files gracefully in production code.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783391986502\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I read a large JSON file in Python without running out of memory?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the ijson library, which parses JSON files incrementally one record at a time without loading the entire file into memory.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783391998750\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>How do I handle missing keys when reading JSON in Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use the .get() method with a default value: data.get(&#8220;key&#8221;, &#8220;default&#8221;). This returns the default instead of raising a KeyError when the key is absent.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783392012635\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Why should I specify encoding when opening a JSON file in Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>JSON files containing non-ASCII characters fail to read correctly without explicit encoding. Always use encoding=&#8221;utf-8&#8243; when opening any text file in Python to handle international characters correctly.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Many Python developers working with APIs, configuration files, and data pipelines encounter JSON as the most common data exchange format. Knowing how to read JSON file in Python correctly, access nested values, handle errors, and work with large files is a foundational skill for backend development, data engineering, and automation. This guide walks through every [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":122084,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717],"tags":[],"views":"26","authorinfo":{"name":"Vishalini Devarajan","url":"https:\/\/www.guvi.in\/blog\/author\/vishalini\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/how-to-read-json-file-in-python-300x117.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/121433"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=121433"}],"version-history":[{"count":3,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/121433\/revisions"}],"predecessor-version":[{"id":122082,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/121433\/revisions\/122082"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/122084"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=121433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=121433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=121433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}