### Prerequisites: * [[Reading and Writing Files in Python]] * [[Arrays in Python]] * [[Dictionaries in Python]] <div style="position: relative; padding-bottom: 74.89597780859917%; height: 0;"><iframe src="https://www.loom.com/embed/babbabc0c2524bdcbe77f03040e46a37?sid=4a963ab2-f1b2-4859-9ccf-be83af1afe52" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div> JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It's commonly used in Python for web development and data exchange. If you're using Python, you'll have seen JSON before as [[Arrays in Python]] and [[Dictionaries in Python]]. They're slightly different, so we use some libraries to easily manipulate them. ### Reading JSON from a File Reading JSON from a file in Python is a straightforward process. Here's a step-by-step guide: 1. **Import the JSON Module**: First, you'll need to import the `json` module in your Python script. ```python import json ``` 2. **Open the File**: Use the `with` statement to open the file. This ensures that the file is properly closed after it's read. ```python ## a common way to do this: with open('data.json', 'r') as file: data = json.load(file) ## another common way a_file = open('data.json', 'r') some_data = json.load(a_file) a_file.close() ## not using the with statement means you need to close the file. ``` 3. **Read the JSON Data**: The `json.load()` function reads the JSON data from the file and converts it into a Python object. 4. **Use the Data**: You can now use the `data` variable as a regular Python object. ### Writing JSON to a File Writing JSON to a file is just as simple: 1. **Create a Python Object**: Define the data you want to write as a Python object, such as a dictionary. ```python data = {'name': 'Liz', 'subject': 'Python'} ``` 2. **Open the File**: Open the file in write mode. ```python with open('output.json', 'w') as file: json.dump(data, file) ``` 3. **Write the JSON Data**: The `json.dump()` function takes the Python object and writes it to the file in JSON format. ### Practical Exercises You can practice these concepts with hands-on exercises available on the [TikTok Python page](https://www.lizthe.dev/tiktok-python). Follow the repls and exercises for a more interactive learning experience. Write your own [[Preparing Data & Analyzing & Synthesizing Data]] engine with ChatGPT. ### Conclusion Reading and writing JSON in Python is a fundamental skill, especially in web development and data handling. By understanding these simple methods, you can easily manipulate JSON data in your Python projects. Feel free to explore more on this topic through your [TikTok videos](https://www.tiktok.com/t/ZTRwkxFaw/) and other resources on your website. Happy coding! 🐍✨ --- **Note**: The code snippets and explanations are based on standard Python practices. For more detailed tutorials and examples, you can refer to [RealPython's JSON guide](https://realpython.com/python-json/) and your TikTok Python content.