{"id":84,"date":"2025-10-21T04:38:36","date_gmt":"2025-10-21T04:38:36","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=84"},"modified":"2025-12-17T07:48:42","modified_gmt":"2025-12-17T07:48:42","slug":"lesson-20-working-with-json-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=84","title":{"rendered":"Lesson 20: Working with JSON in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To learn how to <strong>read, write, and manipulate JSON data<\/strong> in Python using the built-in <code>json<\/code> module.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is JSON?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>JSON (JavaScript Object Notation)<\/strong> is a lightweight format for storing and exchanging data.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is <strong>text-based<\/strong> and human-readable.<\/li>\n\n\n\n<li>Supports <strong>objects (dict)<\/strong>, <strong>arrays (list)<\/strong>, strings, numbers, booleans, and <code>null<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example JSON<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"Sameer\",\n    \"age\": 25,\n    \"skills\": &#91;\"Python\", \"Excel\", \"Web Scraping\"]\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Importing the JSON Module<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. Converting Python to JSON (Serialization)<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>json.dumps()<\/code> to convert Python objects to JSON string.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>data = {\"name\": \"Sameer\", \"age\": 25, \"skills\": &#91;\"Python\", \"Excel\"]}\njson_str = json.dumps(data)\nprint(json_str)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"name\": \"Sameer\", \"age\": 25, \"skills\": &#91;\"Python\", \"Excel\"]}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Save JSON to a file using <code>json.dump()<\/code>:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.json\", \"w\") as f:\n    json.dump(data, f)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf0 <strong>4. Converting JSON to Python (Deserialization)<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Load JSON string to Python dict:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>json_str = '{\"name\": \"Sameer\", \"age\": 25, \"skills\": &#91;\"Python\", \"Excel\"]}'\ndata = json.loads(json_str)\nprint(data&#91;\"name\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Load JSON from a file:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.json\", \"r\") as f:\n    data = json.load(f)\nprint(data&#91;\"skills\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Python', 'Excel']\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>5. Working with Nested JSON<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>nested_json = {\n    \"person\": {\n        \"name\": \"Ali\",\n        \"age\": 22,\n        \"address\": {\"city\": \"Warangal\", \"country\": \"India\"}\n    }\n}\n\n# Access nested data\nprint(nested_json&#91;\"person\"]&#91;\"address\"]&#91;\"city\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Warangal\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>6. Pretty Printing JSON<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>print(json.dumps(data, indent=4))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"Sameer\",\n    \"age\": 25,\n    \"skills\": &#91;\n        \"Python\",\n        \"Excel\"\n    ]\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee0\ufe0f <strong>7. Updating JSON Data<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>data&#91;\"age\"] = 26\ndata&#91;\"skills\"].append(\"Web Scraping\")\n\nwith open(\"data.json\", \"w\") as f:\n    json.dump(data, f, indent=4)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>8. Real-Life Example \u2013 Storing Contact Book<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>contacts = {\n    \"Ali\": {\"phone\": \"9876543210\", \"email\": \"ali@example.com\"},\n    \"Sara\": {\"phone\": \"9123456789\", \"email\": \"sara@example.com\"}\n}\n\n# Save to file\nwith open(\"contacts.json\", \"w\") as f:\n    json.dump(contacts, f, indent=4)\n\n# Load from file\nwith open(\"contacts.json\", \"r\") as f:\n    data = json.load(f)\n\nprint(data&#91;\"Sara\"]&#91;\"phone\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>9123456789<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To learn how to read, write, and manipulate JSON data in Python using the built-in json module. \ud83e\udde9 1. What Is JSON? JSON (JavaScript Object Notation) is a lightweight format for storing and exchanging data. \u2705 Example JSON \u2699\ufe0f 2. Importing the JSON Module \ud83d\udd04 3. Converting Python to JSON (Serialization) Output: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}