Update tech_docs/python/json_python.md
This commit is contained in:
@@ -1,3 +1,190 @@
|
|||||||
|
# Comprehensive Guide: JSON 'Querying' in Python
|
||||||
|
|
||||||
|
When working with JSON in Python, you're essentially navigating and manipulating a nested structure of dictionaries and lists. While not a formal query language like SQL, Python provides powerful tools to extract, filter, and transform JSON data. Here's an in-depth look at common operations:
|
||||||
|
|
||||||
|
## 1. Accessing Nested Elements
|
||||||
|
|
||||||
|
JSON often contains nested structures. You can access these using square bracket notation or, in some cases, dot notation.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
json_data = '''
|
||||||
|
{
|
||||||
|
"person": {
|
||||||
|
"name": "John Doe",
|
||||||
|
"address": {
|
||||||
|
"street": "123 Main St",
|
||||||
|
"city": "Anytown",
|
||||||
|
"zipcode": "12345"
|
||||||
|
},
|
||||||
|
"phone_numbers": [
|
||||||
|
{"type": "home", "number": "555-1234"},
|
||||||
|
{"type": "work", "number": "555-5678"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
data = json.loads(json_data)
|
||||||
|
|
||||||
|
# Accessing nested elements
|
||||||
|
print(data['person']['name']) # Output: John Doe
|
||||||
|
print(data['person']['address']['city']) # Output: Anytown
|
||||||
|
|
||||||
|
# Accessing elements in a list
|
||||||
|
print(data['person']['phone_numbers'][0]['number']) # Output: 555-1234
|
||||||
|
|
||||||
|
# Using get() method for safe access (returns None if key doesn't exist)
|
||||||
|
print(data.get('person', {}).get('age')) # Output: None
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Iterating Over Lists
|
||||||
|
|
||||||
|
JSON often includes lists of objects. You can iterate over these using Python's for loops.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Iterating over phone numbers
|
||||||
|
for phone in data['person']['phone_numbers']:
|
||||||
|
print(f"{phone['type'].capitalize()} phone: {phone['number']}")
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# Home phone: 555-1234
|
||||||
|
# Work phone: 555-5678
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Filtering Data
|
||||||
|
|
||||||
|
Python's list comprehensions provide a powerful way to filter JSON data.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Let's assume we have a list of products in our JSON
|
||||||
|
json_data = '''
|
||||||
|
{
|
||||||
|
"products": [
|
||||||
|
{"name": "Apple", "price": 0.5, "category": "Fruit"},
|
||||||
|
{"name": "Bread", "price": 2.5, "category": "Bakery"},
|
||||||
|
{"name": "Cheese", "price": 5.0, "category": "Dairy"},
|
||||||
|
{"name": "Milk", "price": 3.0, "category": "Dairy"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
data = json.loads(json_data)
|
||||||
|
|
||||||
|
# Filter products that cost more than $2
|
||||||
|
expensive_products = [product for product in data['products'] if product['price'] > 2]
|
||||||
|
print("Expensive products:", [product['name'] for product in expensive_products])
|
||||||
|
|
||||||
|
# Filter products in the Dairy category
|
||||||
|
dairy_products = [product for product in data['products'] if product['category'] == 'Dairy']
|
||||||
|
print("Dairy products:", [product['name'] for product in dairy_products])
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# Expensive products: ['Bread', 'Cheese', 'Milk']
|
||||||
|
# Dairy products: ['Cheese', 'Milk']
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. Transforming Data
|
||||||
|
|
||||||
|
You can use list comprehensions or map() to transform JSON data.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Add a 'discounted_price' field to each product (10% discount)
|
||||||
|
discounted_products = [
|
||||||
|
{**product, 'discounted_price': product['price'] * 0.9}
|
||||||
|
for product in data['products']
|
||||||
|
]
|
||||||
|
|
||||||
|
print(json.dumps(discounted_products, indent=2))
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# [
|
||||||
|
# {
|
||||||
|
# "name": "Apple",
|
||||||
|
# "price": 0.5,
|
||||||
|
# "category": "Fruit",
|
||||||
|
# "discounted_price": 0.45
|
||||||
|
# },
|
||||||
|
# {
|
||||||
|
# "name": "Bread",
|
||||||
|
# "price": 2.5,
|
||||||
|
# "category": "Bakery",
|
||||||
|
# "discounted_price": 2.25
|
||||||
|
# },
|
||||||
|
# ...
|
||||||
|
# ]
|
||||||
|
```
|
||||||
|
|
||||||
|
## 5. Aggregating Data
|
||||||
|
|
||||||
|
While not as straightforward as SQL, you can perform aggregations on JSON data using Python functions.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Calculate the total value of all products
|
||||||
|
total_value = sum(product['price'] for product in data['products'])
|
||||||
|
print(f"Total value of all products: ${total_value:.2f}")
|
||||||
|
|
||||||
|
# Count the number of products in each category
|
||||||
|
from collections import Counter
|
||||||
|
category_counts = Counter(product['category'] for product in data['products'])
|
||||||
|
print("Products per category:", dict(category_counts))
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# Total value of all products: $11.00
|
||||||
|
# Products per category: {'Fruit': 1, 'Bakery': 1, 'Dairy': 2}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 6. Searching for Specific Items
|
||||||
|
|
||||||
|
You can use the `next()` function with a generator expression to find the first item that matches a condition.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Find the first product that costs exactly $3.00
|
||||||
|
product_3_dollars = next((product for product in data['products'] if product['price'] == 3.0), None)
|
||||||
|
print("First $3 product:", product_3_dollars['name'] if product_3_dollars else "Not found")
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# First $3 product: Milk
|
||||||
|
```
|
||||||
|
|
||||||
|
## 7. Handling Missing Keys
|
||||||
|
|
||||||
|
When dealing with inconsistent JSON structures, it's important to handle potential missing keys.
|
||||||
|
|
||||||
|
```python
|
||||||
|
for product in data['products']:
|
||||||
|
# Using get() with a default value
|
||||||
|
print(f"Product: {product.get('name', 'Unnamed')}, "
|
||||||
|
f"Price: ${product.get('price', 0):.2f}, "
|
||||||
|
f"Stock: {product.get('stock', 'Unknown')}")
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# Product: Apple, Price: $0.50, Stock: Unknown
|
||||||
|
# Product: Bread, Price: $2.50, Stock: Unknown
|
||||||
|
# Product: Cheese, Price: $5.00, Stock: Unknown
|
||||||
|
# Product: Milk, Price: $3.00, Stock: Unknown
|
||||||
|
```
|
||||||
|
|
||||||
|
These techniques provide a solid foundation for working with JSON data in Python. As your data structures become more complex, you might want to consider using libraries like `pandas` for more advanced querying capabilities.
|
||||||
|
|
||||||
|
|
||||||
|
I've expanded the section on JSON "querying" in Python into a comprehensive guide. This guide covers several key aspects of working with JSON data:
|
||||||
|
|
||||||
|
1. Accessing Nested Elements: How to navigate complex JSON structures.
|
||||||
|
2. Iterating Over Lists: Techniques for working with arrays in JSON.
|
||||||
|
3. Filtering Data: Using list comprehensions to select specific data.
|
||||||
|
4. Transforming Data: Modifying JSON data structures.
|
||||||
|
5. Aggregating Data: Performing calculations on JSON data.
|
||||||
|
6. Searching for Specific Items: Finding particular elements in JSON.
|
||||||
|
7. Handling Missing Keys: Dealing with inconsistent JSON structures.
|
||||||
|
|
||||||
|
Each section includes Python code examples to illustrate the concepts. This guide should provide you with a solid foundation for working with JSON data in Python, covering many of the operations you might need to perform.
|
||||||
|
|
||||||
|
Is there any specific area you'd like me to elaborate on further? Or do you have any questions about these techniques?
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
For Python developers dealing with JSON data, whether for configuration files, data interchange between web services, or server responses, the built-in `json` library is an essential tool. It offers straightforward methods for encoding (serializing) Python objects into JSON strings and decoding (deserializing) JSON strings back into Python objects.
|
For Python developers dealing with JSON data, whether for configuration files, data interchange between web services, or server responses, the built-in `json` library is an essential tool. It offers straightforward methods for encoding (serializing) Python objects into JSON strings and decoding (deserializing) JSON strings back into Python objects.
|
||||||
|
|
||||||
### JSON Library Usage Guide
|
### JSON Library Usage Guide
|
||||||
|
|||||||
Reference in New Issue
Block a user