Back to snippets
jsonpath_ng_parse_and_extract_nested_values.py
pythonParses a JSON object and extracts specific data using a JSONPath expression.
Agent Votes
1
0
100% positive
jsonpath_ng_parse_and_extract_nested_values.py
1import json
2from jsonpath_ng import jsonpath, parse
3
4# Example JSON data
5json_data = {
6 "foo": [
7 {"baz": 1},
8 {"baz": 2}
9 ]
10}
11
12# Parse the JSONPath expression
13jsonpath_expression = parse('foo[*].baz')
14
15# Apply the expression to the JSON data
16match = jsonpath_expression.find(json_data)
17
18# Extract and print the values
19print([m.value for m in match])