ds_protocol_graphql_py_lib.utils.json_utils¶
File: json_utils.py
Region: ds_protocol_graphql_py_lib/utils/json_utils
Restricted dotted-path helpers for GraphQL response JSON.
Paths are object members and list indexes separated by dots
(data.customers.edges, data.items.0.id). Negative indexes count from
the end (data.customers.edges.-1.cursor is the last edge’s cursor).
$ or "" is the root. No wildcards, filters, slices, or recursive descent.
Example
>>> body = {"data": {"customers": {"edges": [{"cursor": "a"}]}}}
>>> get_path(body, "data.customers.edges.0.cursor")
'a'
>>> get_path(body, "data.customers.edges.-1.cursor")
'a'
>>> get_list(body, "data.customers.edges")
[{'cursor': 'a'}]
Attributes¶
Functions¶
|
Like |
|
Read a value via dotted path. |
|
Read a list at |
Module Contents¶
- ds_protocol_graphql_py_lib.utils.json_utils._MISSING: Any¶
- ds_protocol_graphql_py_lib.utils.json_utils._resolve(data: Any, path: str) Any[source]¶
Like
get_path(), but returns_MISSING(notNone) when the path does not exist.
- ds_protocol_graphql_py_lib.utils.json_utils.get_path(data: Any, path: str) Any[source]¶
Read a value via dotted path.
- Parameters:
data – Parsed JSON value.
path – Dotted path;
$or""returnsdataitself. List segments are integer indexes; negative indexes count from the end.
- Returns:
The value at
path, orNonewhen any segment is missing or a list index is out of range (e.g.-1on an empty list).
- ds_protocol_graphql_py_lib.utils.json_utils.get_list(data: Any, path: str) list[Any][source]¶
Read a list at
path.A
nulllist is an empty list: GraphQL APIs commonly returnnullinstead of[]for an empty nullable list (Visma Business NXT does). A missing path, including one whose parent isnull, is an error, so a typo in the path fails loudly instead of reading as “no rows”.- Parameters:
data – Parsed JSON value.
path – Dotted path to a list.
- Returns:
The list at
path, or[]when the value there isnull.- Raises:
ValueError – If the path is missing.
TypeError – If the path resolves to a non-list value.