Backend Development 6 min read

Understanding JSONPath Decorator in Python

This article explains how to create and use a Python JSONPath decorator to simplify extracting data from JSON, covering installation, basic and advanced use cases such as list handling, filtering, aggregation, asynchronous processing, API parsing, validation, dynamic path generation, and nested data extraction, with full code examples.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding JSONPath Decorator in Python

JSONPath is an expression language for extracting data from JSON documents, similar to XPath for XML. By wrapping JSONPath queries in a Python decorator, developers can make code more readable, reusable, and concise.

Typical use cases include API response parsing, data validation, filtering, aggregation, and asynchronous processing of large JSON payloads.

pip install jsonpath-ng

from jsonpath_ng import parse def jsonpath_query(jsonpath_expression): def decorator(func): def wrapper(json_data): jsonpath_expr = parse(jsonpath_expression) matches = [match.value for match in jsonpath_expr.find(json_data)] return matches return wrapper return decorator @jsonpath_query('$.store.book[*].title') def extract_titles(json_data): return json_data data = { "store": { "book": [ {"category": "reference", "title": "The C Programming Language"}, {"category": "fiction", "title": "The Great Gatsby"} ] } } titles = extract_titles(data) print(titles)

@jsonpath_query('$.store.book[?(@.isbn)]') def extract_books_with_isbn(json_data): return json_data data = { "store": { "book": [ {"category": "reference", "title": "The C Programming Language"}, {"category": "fiction", "title": "The Great Gatsby", "isbn": "123456"} ] } } books_with_isbn = extract_books_with_isbn(data) print(books_with_isbn)

@jsonpath_query('$.store.book[?(@.price < 10)]') def extract_cheap_books(json_data): return json_data data = { "store": { "book": [ {"category": "reference", "title": "The C Programming Language", "price": 30}, {"category": "fiction", "title": "The Great Gatsby", "price": 5} ] } } cheap_books = extract_cheap_books(data) print(cheap_books)

@jsonpath_query('sum($.store.book[*].price)') def calculate_total_price(json_data): return json_data data = { "store": { "book": [ {"category": "reference", "title": "The C Programming Language", "price": 30}, {"category": "fiction", "title": "The Great Gatsby", "price": 5} ] } } total_price = calculate_total_price(data) print(total_price)

import asyncio import aiohttp async def fetch_json(url): async with aiohttp.ClientSession() as session: async with session.get(url) as response: return await response.json() @jsonpath_query('$.items[?(@.id == 1)].name') async def extract_item_name(json_data): return json_data async def main(): data = await fetch_json('https://api.example.com/data') item_name = await extract_item_name(data) print(item_name) asyncio.run(main())

import requests @jsonpath_query('$.message') def parse_api_message(json_data): return json_data response = requests.get('https://api.example.com/status') data = response.json() message = parse_api_message(data) print(message)

@jsonpath_query('$.store.book[*]') def validate_books(json_data): if not json_data: raise ValueError("No books found") return json_data data = {"store": {"book": []}} try: books = validate_books(data) except ValueError as e: print(e)

def generate_jsonpath(key): return f'$.store.book[*].{key}' @jsonpath_query(generate_jsonpath('title')) def extract_titles(json_data): return json_data data = {"store": {"book": [{"category": "reference", "title": "The C Programming Language"}, {"category": "fiction", "title": "The Great Gatsby"}]}} titles = extract_titles(data) print(titles)

@jsonpath_query('$.store.book[*].title, $.store.book[*].price') def extract_titles_and_prices(json_data): return json_data data = {"store": {"book": [{"category": "reference", "title": "The C Programming Language", "price": 30}, {"category": "fiction", "title": "The Great Gatsby", "price": 5}]}} titles_and_prices = extract_titles_and_prices(data) print(titles_and_prices)

@jsonpath_query('$.store.book[*].author[*]') def extract_authors(json_data): return json_data data = {"store": {"book": [{"category": "reference", "title": "The C Programming Language", "author": [{"name": "Brian W. Kernighan"}, {"name": "Dennis M. Ritchie"}]}, {"category": "fiction", "title": "The Great Gatsby", "author": [{"name": "F. Scott Fitzgerald"}]}]}} authors = extract_authors(data) print(authors)

These examples demonstrate how a JSONPath decorator can streamline data extraction, validation, aggregation, and asynchronous processing in Python, making it valuable for API development, data pipelines, and automated testing.

PythonData ProcessingAPIJsonPathasyncdecorator
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.