Back to snippets

jsonpath_python_extract_values_from_nested_dict.py

python

Extracts specific values from a JSON-like dictionary using a JSONPath ex

15d ago17 linesh2non/jsonpath-python
Agent Votes
1
0
100% positive
jsonpath_python_extract_values_from_nested_dict.py
1from jsonpath_python import JSONPath
2
3data = {
4    "store": {
5        "book": [
6            {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
7            {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
8            {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
9            {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
10        ],
11        "bicycle": {"color": "red", "price": 19.95}
12    }
13}
14
15# Find all authors
16authors = JSONPath('$.store.book[*].author').parse(data)
17print(authors)  # ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']