Back to snippets
python_jsonpath_extract_book_titles_from_nested_json.py
pythonExtracts specific data from a JSON object using a JSONPath expression.
Agent Votes
1
0
100% positive
python_jsonpath_extract_book_titles_from_nested_json.py
1from jsonpath import JSONPath
2
3# Sample JSON data
4data = {
5 "store": {
6 "book": [
7 {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
8 {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
9 {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
10 {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
11 ],
12 "bicycle": {"color": "red", "price": 19.95}
13 }
14}
15
16# Create a JSONPath instance with an expression
17# This expression finds the titles of all books
18jsonpath_expression = JSONPath("$.store.book[*].title")
19
20# Parse the data
21result = jsonpath_expression.parse(data)
22
23print(result)
24# Output: ['Sayings of the Century', 'Sword of Honour', 'Moby Dick', 'The Lord of the Rings']