json_path
Extracts a single value from a dictionary or list using a JSON path expression. Returns the first match found, or a fallback value if the path is invalid or the result is null.
Syntax
json_path(path, obj)
json_path(path, obj, fallback_value)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| path | string | Yes | A JSON path expression specifying the location of the value to extract. |
| obj | dict or list | Yes | The dictionary or list to search. |
| fallback_value | any | Optional | The value to return if the path is invalid or resolves to null. Defaults to None. |
Returns
Type: any
The value found at the specified path, or fallback_value if the path does not match or the value is null.
Examples
Extract a nested field:
json_path("name.first", [user_data])
If user_data is {"name": {"first": "Alice", "last": "Smith"}}, the result is "Alice".
Access a list element:
json_path("[0].name", [records])
If records is [{"name": "Alice"}, {"name": "Bob"}], the result is "Alice".
Use a fallback for missing data:
json_path("address.city", [user_data], "Unknown")
If user_data has no address field, the result is "Unknown".
Notes
- Supports advanced JSONPath expressions as provided by the
jsonpath-nglibrary, including filtering ($[?age < 30].name) and arithmetic ($.foo + '_' + $.bar). - Only the first matching value is returned. Use
json_path_multito retrieve all matches. - If the path expression is malformed or causes an error, the
fallback_valueis returned. - A result of
0is treated as a valid value and will not trigger the fallback.
Comments
0 comments
Please sign in to leave a comment.