Day 45 of the 100 Days of Python series – Reading and writing JSON in Python
Welcome to Day 45. In this installment we will see how to work with JSON (JavaScript Object Notation) in Python, a lightweight data format widely used in APIs, configuration files, and the exchange of information between systems.
What is JSON: JSON is a lightweight format for data exchange. It is human-readable and does not depend on a specific language. Its key-value pair structure is similar to Python dictionaries.
Example of an illustrative JSON structure: { name: Alice, age: 25, skills: [ Python, Data Science ] }
Python's json module: Python includes the json module to parse JSON into Python objects and to convert Python objects into JSON strings. For example, import json is the way to access those functions.
Reading JSON (deserialization): To read JSON from a file and convert it into a Python object, json.load is used. Illustrative example without quotes: with open(data.json, r) as file: data = json.load(file) print(data) print(type(data)) expected result dict.
Key method: json.load(file) reads JSON from a file and converts it into a Python object.
Writing JSON (serialization): To write Python objects to a JSON file, json.dump is used. Illustrative example: data = { name: Alice, age: 25, skills: [ Python, Data Science ] } with open(data.json, w) as file: json.dump(data, file, indent=4) The indent parameter makes the file human-readable.
Key methods: json.dump(obj, file, indent=4) writes in JSON format. Additionally, json.dumps exists to obtain a JSON string in memory, and json.loads to convert a JSON string into a Python object.
Conversions with strings: You often receive JSON from an API as a string. To convert a string into a Python object, use json.loads. Illustrative example: json_string = { name: Bob, age: 30 } data = json.loads(json_string) print(data[name]) To convert a Python object into a JSON string, use json.dumps. Example: data = { name: Charlie, age: 22 } json_str = json.dumps(data, indent=2) print(json_str)
Common pitfalls: JSON keys must be text strings. JSON does not directly store Python-specific objects such as sets or functions. To serialize special types like datetime, you can use default=str in json.dump or json.dumps.
Mini practical illustrative example: import json user = { username: tech_guru, active: True, followers: 1050 } # Save to file with open(user.json, w) as f: json.dump(user, f, indent=4) # Read from file with open(user.json, r) as f: loaded_user = json.load(f) print(loaded_user)
Quick notes: Serialization means Python to JSON using dump and dumps. Deserialization means JSON to Python using load and loads. Always handle file operations with with to ensure proper closing.
Proposed challenge: Create a Python program that reads user data (name, age, and skills) from input, saves it to a JSON file, and then reads the file and prints the stored details.
About Q2BSTUDIO: Q2BSTUDIO is a software development company that offers custom applications and custom software for companies in all sectors. We are specialists in artificial intelligence and AI for businesses, we develop custom AI agents and Power BI solutions for reporting and advanced visualization. We also provide AWS and Azure cloud services, business intelligence services, and comprehensive cybersecurity support to protect applications and data. Our team combines experience in custom application development with artificial intelligence and cybersecurity capabilities to deliver scalable and secure solutions.
Why choose Q2BSTUDIO: we deliver custom software that incorporates artificial intelligence to automate processes and improve decision-making. We implement AI agents that integrate with existing systems, provide business intelligence services to transform data into value, and deploy infrastructures on AWS and Azure cloud services with cybersecurity practices from the design phase. If you are looking for custom applications, custom software, artificial intelligence, cybersecurity, AWS and Azure cloud services, business intelligence services, AI for businesses, AI agents, or Power BI, we can help you.
If you want us to adapt the JSON example to a real case in your business or to integrate that functionality with an AWS and Azure cloud service or with an artificial intelligence and Power BI solution, contact Q2BSTUDIO and we will advise you on the best architecture and on the secure and efficient implementation.


