diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 5ea5081..8f4f26a 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -1,3 +1,4 @@ # List of sections - [Decorators/\*args/**kwargs](decorator-kwargs-args.md) +- [JSON module](json-module.md) diff --git a/contrib/advanced-python/json-module.md b/contrib/advanced-python/json-module.md new file mode 100644 index 0000000..20a9408 --- /dev/null +++ b/contrib/advanced-python/json-module.md @@ -0,0 +1,294 @@ +# JSON Module + +## What is JSON? + +- JSON (JavaScript Object Notation) is a format for structuring data. +- JSON is a lightweight, text-based data interchange format that is completely language-independent. +- Similar to XML, JSON is a format for structuring data commonly used by web applications to communicate with each other. + +## Why JSON? + +- Whenever we declare a variable and assign a value to it, the variable itself doesn't hold the value. Instead, the variable holds an address in memory where the value is stored. For example: + +```python +age = 21 +``` + +- When we use `age`, it gets replaced with `21`. However, age doesn't contain 21, it contains the address of the memory location where 21 is stored. + +- While this works locally, transferring this data, such as through an API, poses a challenge. Sending your computer’s entire memory with the addresses is impractical and insecure. This is where JSON comes to the rescue. + +### Example JSON + +- JSON supports most widely used data types including String + , Number, Boolean, Null, Array and Object. +- Here is an example of JSON file + +```json +{ + "name": "John Doe", + "age": 21, + "isStudent": true, + "address": null, + "courses": ["Math", "Science", "History"], + "grades": { + "Math": 95, + "Science": 89, + "History": 76 + } +} +``` + +# Python JSON + +Python too supports JSON with a built-in package called `json`. This package provides all the necessary tools for working with JSON Objects including `parsing, serializing, deserializing, and many more`. + +## 1. Python parse JSON string. + +- To parse JSON string Python firstly we import the JSON module. +- JSON string is converted to a Python object using `json.loads()` method of JSON module in Python. +- Example Code: + +```python +# Python program to convert JSON to Python +import json + +# JSON string +students ='{"id":"01", "name": "Yatharth", "department":"Computer Science Engineering"}' + +# Convert string to Python dict +students_dict = json.loads(students) +print(students_dict) + +print(students_dict['name']) + +``` + +- Ouput: + +```json +{'id': '01', 'name': 'Yatharth', 'department': 'Computer Science Engineering'} +Yatharth +``` + +## 2. Python load JSON file. + +- JSON data can also be directly fetch from a json file +- Example: + +```python +import json +# Opening JSON file +f = open('input.json',) + +# Returns JSON object as a dictionary +data = json.load(f) + +# Iterating through the json file +for i in data['students']: + print(i) + +# Closing file +f.close() +``` + +- JSON file + +```json +{ + "students":{ + { + "id": "01", + "name": "Yatharth", + "department": "Computer Science Engineering" + }, + { + "id": "02", + "name": "Raj", + "department": "Mechanical Engineering" + } + } +} +``` + +- Ouput + +```json +{'id': '01', 'name': 'Yatharth', 'department': 'Computer Science Engineering'} +{'id': '02', 'name': 'Raj', 'department': 'Mechanical Engineering'} +``` +- `json.load()`: Reads JSON data from a file object and deserializes it into a Python object. +- `json.loads()`: Deserializes JSON data from a string into a Python object. +