0% found this document useful (0 votes)
80 views3 pages

Json Notes

This document provides a quick reference guide to using JSON in Python, covering its definition, syntax, and the json module for encoding and decoding JSON data. It explains how to load and save JSON data, the differences between json.load() and json.loads(), and common issues encountered when working with JSON. Additionally, it highlights real-life use cases for JSON, such as storing configuration data and data exchange between applications.

Uploaded by

newone1804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views3 pages

Json Notes

This document provides a quick reference guide to using JSON in Python, covering its definition, syntax, and the json module for encoding and decoding JSON data. It explains how to load and save JSON data, the differences between json.load() and json.loads(), and common issues encountered when working with JSON. Additionally, it highlights real-life use cases for JSON, such as storing configuration data and data exchange between applications.

Uploaded by

newone1804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧠 JSON in Python: Quick Reference Notes

🔹 What is JSON?

 JavaScript Object Notation: lightweight data-interchange format.

 Syntax: resembles Python dictionaries but uses double quotes for keys/strings.

 Commonly used for APIs, config files, and data exchange.

🟡 Active Recall: What is the difference between JSON and Python dict syntax?

→ JSON uses double quotes for keys and strings, while Python dicts can use single or double quotes.

📚 Python's json Module

Python’s standard library includes the json module for encoding and decoding JSON data.

🔸 Importing the Module

import json

🟡 Active Recall: Which Python module is used to work with JSON data?

→ The json module.

📥 Loading JSON (Deserialization)

1. From a JSON String

json_str = '{"name": "Divi", "age": 23}'

data = [Link](json_str) # Returns a Python dict

2. From a JSON File

with open('[Link]') as f:

data = [Link](f)

# File reading + JSON parsing

🟡 Active Recall: What's the difference between [Link]() and [Link]()?

→ [Link]() reads and parses JSON from a file; [Link]() parses from a string.

1. Which functions are used for converting Python objects into JSON?
→ [Link]() for files, [Link]() for strings.

2. How do you make a JSON file human-readable in Python?


→ Use indent=4 in [Link]() or [Link]().
3. What gets converted to null in JSON when serializing Python data?
→ Python’s None becomes null.

4. Can you name 3 situations where JSON files are commonly used?

 Storing configuration data ([Link])

 Saving results from web scraping

 Data exchange between frontend and backend apps

Would you like a printable version of these, or maybe turned into a Tkinter-based flashcard app for
quick revision during coding sessions? I think you’d enjoy that!

📤 Saving JSON (Serialization)

1. To JSON String

data = {"name": "Divi", "age": 23}

json_str = [Link](data)

2. To JSON File

with open('[Link]', 'w') as f:

[Link](data, f)

🟡 Active Recall: Which functions are used for converting Python objects into JSON?

→ [Link]() for files, [Link]() for strings

⚙️Optional Parameters in dump() and dumps()

 indent=4 → pretty print

 sort_keys=True → alphabetical order

 ensure_ascii=False → allow Unicode

[Link](data, indent=4, sort_keys=True)

🟡 Active Recall: How do you make a JSON file human-readable in Python?

→ Use indent=4 in [Link]() or [Link]().

📌 Common Issues
 JSON keys/strings must use double quotes

 Python None → JSON null

 Booleans: True → true, False → false

 Lists and dicts must be properly nested

🟡 Active Recall: What gets converted to null in JSON when serializing Python data?

→ Python’s None becomes null.

🧪 Real-Life Use Cases

 Configurations: [Link]

 Storing scraped data

 Exchanging data between frontend & backend

🟡 Active Recall: Can you name 3 situations where JSON files are commonly used?

 Storing configuration data ([Link])

 Saving results from web scraping

 Data exchange between frontend and backend apps

You might also like