Skip to content

glide_sync.sync_commands.glide_json

Glide module for JSON commands.

Examples:

>>> from glide import glide_json
>>> import json
>>> value = {'a': 1.0, 'b': 2}
>>> json_str = glide_json.dumps(value) # Convert Python dictionary to JSON string using json.dumps()
>>> glide_json.set(client, "doc", "$", json_str)
    'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
>>> json_get = glide_json.get(client, "doc", "$") # Returns the value at path '$' in the JSON document stored at
                                                        # `doc` as JSON string.
>>> print(json_get)
    b"[{"a":1.0,"b":2}]"
>>> json.loads(str(json_get))
    [{"a": 1.0, "b" :2}] # JSON object retrieved from the key `doc` using json.loads()

set(client, key, path, value, set_condition=None)

Sets the JSON value at the specified path stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

Represents the path within the JSON document where the value will be set. The key will be modified only if value is added as the last child in the specified path, or if the specified path acts as the parent of a new child being added.

required
value TEncodable

The value to set at the specific path, in JSON formatted bytes or str.

required
set_condition Optional[ConditionalChange]

Set the value only if the given condition is met (within the key or path). Equivalent to [XX | NX] in the RESP API. Defaults to None.

None

Returns:

Type Description
Optional[TOK]

Optional[TOK]: If the value is successfully set, returns OK. If value isn't set because of set_condition, returns None.

Examples:

>>> from glide import glide_json
>>> import json
>>> value = {'a': 1.0, 'b': 2}
>>> json_str = json.dumps(value)
>>> glide_json.set(client, "doc", "$", json_str)
    'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def set(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
    value: TEncodable,
    set_condition: Optional[ConditionalChange] = None,
) -> Optional[TOK]:
    """
    Sets the JSON value at the specified `path` stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): Represents the path within the JSON document where the value will be set.
            The key will be modified only if `value` is added as the last child in the specified `path`, or if the specified
            `path` acts as the parent of a new child being added.
        value (TEncodable): The value to set at the specific path, in JSON formatted bytes or str.
        set_condition (Optional[ConditionalChange]): Set the value only if the given condition is met (within the key or path).
            Equivalent to [`XX` | `NX`] in the RESP API. Defaults to None.

    Returns:
        Optional[TOK]: If the value is successfully set, returns OK.
            If `value` isn't set because of `set_condition`, returns None.

    Examples:
        >>> from glide import glide_json
        >>> import json
        >>> value = {'a': 1.0, 'b': 2}
        >>> json_str = json.dumps(value)
        >>> glide_json.set(client, "doc", "$", json_str)
            'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
    """
    args = ["JSON.SET", key, path, value]
    if set_condition:
        args.append(set_condition.value)

    return cast(Optional[TOK], client.custom_command(args))

get(client, key, paths=None, options=None)

Retrieves the JSON value at the specified `paths` stored at `key`.

Args:
    client (TGlideClient): The client to execute the command.
    key (TEncodable): The key of the JSON document.
    paths (Optional[Union[TEncodable, List[TEncodable]]]): The path or list of paths within the JSON document.
        Default to None.
    options (Optional[JsonGetOptions]): Options for formatting the byte representation of the JSON data.
        See `JsonGetOptions`.

Returns:
    TJsonResponse[Optional[bytes]]:
        If one path is given:
            For JSONPath (path starts with `$`):
                Returns a stringified JSON list of bytes replies for every possible path,
                or a byte string representation of an empty array, if path doesn't exists.
                If `key` doesn't exist, returns None.
            For legacy path (path doesn't start with `$`):
                Returns a byte string representation of the value in `path`.
                If `path` doesn't exist, an error is raised.
                If `key` doesn't exist, returns None.
        If multiple paths are given:
            Returns a stringified JSON object in bytes, in which each path is a key, and it's corresponding value, is the
            value as if the path was executed in the command as a single path.
    In case of multiple paths, and `paths` are a mix of both JSONPath and legacy path, the command behaves as if all are
    JSONPath paths.
    For more information about the returned type, see `TJsonResponse`.

Examples:
    >>> from glide import glide_json, JsonGetOptions
    >>> import json
    >>> json_str = glide_json.get(client, "doc", "$")
    >>> json.loads(str(json_str)) # Parse JSON string to Python data
        [{"a": 1.0, "b" :2}]  # JSON object retrieved from the key `doc` using json.loads()
    >>> glide_json.get(client, "doc", "$")
        b"[{"a":1.0,"b":2}]"  # Returns the value at path '$' in the JSON document stored at `doc`.
    >>> glide_json.get(client, "doc", ["$.a", "$.b"], JsonGetOptions(indent="  ", newline="

", space=" ")) b"{ "$.a": [ 1.0 ], "$.b": [ 2 ] }" # Returns the values at paths '$.a' and '$.b' in the JSON # document stored at doc, with specified # formatting options. >>> glide_json.get(client, "doc", "$.non_existing_path") b"[]" # Returns an empty array since the path '$.non_existing_path' does not exist in the JSON document # stored at doc.

Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def get(
    client: TGlideClient,
    key: TEncodable,
    paths: Optional[Union[TEncodable, List[TEncodable]]] = None,
    options: Optional[JsonGetOptions] = None,
) -> TJsonResponse[Optional[bytes]]:
    """
    Retrieves the JSON value at the specified `paths` stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        paths (Optional[Union[TEncodable, List[TEncodable]]]): The path or list of paths within the JSON document.
            Default to None.
        options (Optional[JsonGetOptions]): Options for formatting the byte representation of the JSON data.
            See `JsonGetOptions`.

    Returns:
        TJsonResponse[Optional[bytes]]:
            If one path is given:
                For JSONPath (path starts with `$`):
                    Returns a stringified JSON list of bytes replies for every possible path,
                    or a byte string representation of an empty array, if path doesn't exists.
                    If `key` doesn't exist, returns None.
                For legacy path (path doesn't start with `$`):
                    Returns a byte string representation of the value in `path`.
                    If `path` doesn't exist, an error is raised.
                    If `key` doesn't exist, returns None.
            If multiple paths are given:
                Returns a stringified JSON object in bytes, in which each path is a key, and it's corresponding value, is the
                value as if the path was executed in the command as a single path.
        In case of multiple paths, and `paths` are a mix of both JSONPath and legacy path, the command behaves as if all are
        JSONPath paths.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json, JsonGetOptions
        >>> import json
        >>> json_str = glide_json.get(client, "doc", "$")
        >>> json.loads(str(json_str)) # Parse JSON string to Python data
            [{"a": 1.0, "b" :2}]  # JSON object retrieved from the key `doc` using json.loads()
        >>> glide_json.get(client, "doc", "$")
            b"[{\"a\":1.0,\"b\":2}]"  # Returns the value at path '$' in the JSON document stored at `doc`.
        >>> glide_json.get(client, "doc", ["$.a", "$.b"], JsonGetOptions(indent="  ", newline="\n", space=" "))
            b"{\n \"$.a\": [\n  1.0\n ],\n \"$.b\": [\n  2\n ]\n}"  # Returns the values at paths '$.a' and '$.b' in the JSON
                                                                    # document stored at `doc`, with specified
                                                                    # formatting options.
        >>> glide_json.get(client, "doc", "$.non_existing_path")
            b"[]"  # Returns an empty array since the path '$.non_existing_path' does not exist in the JSON document
                   # stored at `doc`.
    """
    args = ["JSON.GET", key]
    if options:
        args.extend(options.get_options())
    if paths:
        if isinstance(paths, (str, bytes, bytearray, memoryview)):
            paths = [paths]
        args.extend(paths)

    return cast(TJsonResponse[Optional[bytes]], client.custom_command(args))

arrappend(client, key, path, values)

Appends one or more values to the JSON array at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

Represents the path within the JSON document where the values will be appended.

required
values TEncodable

The values to append to the JSON array at the specified path. JSON string values must be wrapped with quotes. For example, to append "foo", pass ""foo"".

required

Returns:

Type Description
TJsonResponse[int]

TJsonResponse[int]: For JSONPath (path starts with $): Returns a list of integer replies for every possible path, indicating the new length of the array after appending values, or None for JSON values matching the path that are not an array. If path doesn't exist, an empty array will be returned. For legacy path (path doesn't start with $): Returns the length of the array after appending values to the array at path. If multiple paths match, the length of the first updated array is returned. If the JSON value at path is not a array or if path doesn't exist, an error is raised. If key doesn't exist, an error is raised.

TJsonResponse[int]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> import json
>>> glide_json.set(client, "doc", "$", '{"a": 1, "b": ["one", "two"]}')
    'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
>>> glide_json.arrappend(client, "doc", ["three"], "$.b")
    [3]  # Returns the new length of the array at path '$.b' after appending the value.
>>> glide_json.arrappend(client, "doc", ["four"], ".b")
    4 # Returns the new length of the array at path '.b' after appending the value.
>>> json.loads(glide_json.get(client, "doc", "."))
    {"a": 1, "b": ["one", "two", "three", "four"]}  # Returns the updated JSON document
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def arrappend(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
    values: List[TEncodable],
) -> TJsonResponse[int]:
    """
    Appends one or more `values` to the JSON array at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): Represents the path within the JSON document where the `values` will be appended.
        values (TEncodable): The values to append to the JSON array at the specified path.
            JSON string values must be wrapped with quotes. For example, to append `"foo"`, pass `"\"foo\""`.

    Returns:
        TJsonResponse[int]:
            For JSONPath (`path` starts with `$`):
                Returns a list of integer replies for every possible path, indicating the new length of the array after
                appending `values`,
                or None for JSON values matching the path that are not an array.
                If `path` doesn't exist, an empty array will be returned.
            For legacy path (`path` doesn't start with `$`):
                Returns the length of the array after appending `values` to the array at `path`.
                If multiple paths match, the length of the first updated array is returned.
                If the JSON value at `path` is not a array or if `path` doesn't exist, an error is raised.
            If `key` doesn't exist, an error is raised.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> import json
        >>> glide_json.set(client, "doc", "$", '{"a": 1, "b": ["one", "two"]}')
            'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
        >>> glide_json.arrappend(client, "doc", ["three"], "$.b")
            [3]  # Returns the new length of the array at path '$.b' after appending the value.
        >>> glide_json.arrappend(client, "doc", ["four"], ".b")
            4 # Returns the new length of the array at path '.b' after appending the value.
        >>> json.loads(glide_json.get(client, "doc", "."))
            {"a": 1, "b": ["one", "two", "three", "four"]}  # Returns the updated JSON document
    """
    args = ["JSON.ARRAPPEND", key, path] + values
    return cast(TJsonResponse[int], client.custom_command(args))

arrindex(client, key, path, value, options=None)

Searches for the first occurrence of a scalar JSON value (i.e., a value that is neither an object nor an array) within arrays at the specified path in the JSON document stored at key.

If specified, options.start and options.end define an inclusive-to-exclusive search range within the array. (Where options.start is inclusive and options.end is exclusive).

Out-of-range indices adjust to the nearest valid position, and negative values count from the end (e.g., -1 is the last element, -2 the second last).

Setting options.end to 0 behaves like -1, extending the range to the array's end (inclusive).

If options.start exceeds options.end, -1 is returned, indicating that the value was not found.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

The path within the JSON document.

required
value TEncodable

The value to search for within the arrays.

required
options Optional[JsonArrIndexOptions]

Options specifying an inclusive start index and an optional exclusive end index for a range-limited search. Defaults to the full array if not provided. See JsonArrIndexOptions.

None

Returns:

Type Description
TJsonResponse[int]

Optional[TJsonResponse[int]]: For JSONPath (path starts with $): Returns an array of integers for every possible path, indicating of the first occurrence of value within the array, or None for JSON values matching the path that are not an array. A returned value of -1 indicates that the value was not found in that particular array. If path does not exist, an empty array will be returned. For legacy path (path doesn't start with $): Returns an integer representing the index of the first occurrence of value within the array at the specified path. A returned value of -1 indicates that the value was not found in that particular array. If multiple paths match, the index of the value from the first matching array is returned. If the JSON value at the path is not an array or if path does not exist, an error is raised. If key does not exist, an error is raised.

TJsonResponse[int]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"], ["a", "b", "c"]]')
    'OK'
>>> glide_json.arrindex(client, "doc", "$[*]", '"b"')
    [-1, -1, 1, 1]
>>> glide_json.set(client, "doc", ".", '{"children": ["John", "Jack", "Tom", "Bob", "Mike"]}')
    'OK'
>>> glide_json.arrindex(client, "doc", ".children", '"Tom"')
    2
>>> glide_json.set(client, "doc", "$", '{"fruits": ["apple", "banana", "cherry", "banana", "grape"]}')
    'OK'
>>> glide_json.arrindex(client, "doc", "$.fruits", '"banana"', JsonArrIndexOptions(start=2, end=4))
    3
>>> glide_json.set(client, "k", ".", '[1, 2, "a", 4, "a", 6, 7, "b"]')
    'OK'
>>> glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=0))
    7  # "b" found at index 7 within the specified range, treating end=0 as the entire array's end.
>>> glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=-1))
    7  # "b" found at index 7, with end=-1 covering the full array to its last element.
>>> glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=7))
    -1  # "b" not found within the range from index 4 to exclusive end at index 7.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def arrindex(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
    value: TEncodable,
    options: Optional[JsonArrIndexOptions] = None,
) -> TJsonResponse[int]:
    """
    Searches for the first occurrence of a scalar JSON value (i.e., a value that is neither an object nor an array) within
    arrays at the specified `path` in the JSON document stored at `key`.

    If specified, `options.start` and `options.end` define an inclusive-to-exclusive search range within the array.
    (Where `options.start` is inclusive and `options.end` is exclusive).

    Out-of-range indices adjust to the nearest valid position, and negative values count from the end (e.g., `-1` is the last
    element, `-2` the second last).

    Setting `options.end` to `0` behaves like `-1`, extending the range to the array's end (inclusive).

    If `options.start` exceeds `options.end`, `-1` is returned, indicating that the value was not found.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): The path within the JSON document.
        value (TEncodable): The value to search for within the arrays.
        options (Optional[JsonArrIndexOptions]): Options specifying an inclusive `start` index and an optional exclusive `end`
            index for a range-limited search.
            Defaults to the full array if not provided. See `JsonArrIndexOptions`.

    Returns:
        Optional[TJsonResponse[int]]:
            For JSONPath (`path` starts with `$`):
                Returns an array of integers for every possible path, indicating of the first occurrence of `value`
                within the array,
                or None for JSON values matching the path that are not an array.
                A returned value of `-1` indicates that the value was not found in that particular array.
                If `path` does not exist, an empty array will be returned.
            For legacy path (`path` doesn't start with `$`):
                Returns an integer representing the index of the first occurrence of `value` within the array at the
                specified path.
                A returned value of `-1` indicates that the value was not found in that particular array.
                If multiple paths match, the index of the value from the first matching array is returned.
                If the JSON value at the `path` is not an array or if `path` does not exist, an error is raised.
            If `key` does not exist, an error is raised.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"], ["a", "b", "c"]]')
            'OK'
        >>> glide_json.arrindex(client, "doc", "$[*]", '"b"')
            [-1, -1, 1, 1]
        >>> glide_json.set(client, "doc", ".", '{"children": ["John", "Jack", "Tom", "Bob", "Mike"]}')
            'OK'
        >>> glide_json.arrindex(client, "doc", ".children", '"Tom"')
            2
        >>> glide_json.set(client, "doc", "$", '{"fruits": ["apple", "banana", "cherry", "banana", "grape"]}')
            'OK'
        >>> glide_json.arrindex(client, "doc", "$.fruits", '"banana"', JsonArrIndexOptions(start=2, end=4))
            3
        >>> glide_json.set(client, "k", ".", '[1, 2, "a", 4, "a", 6, 7, "b"]')
            'OK'
        >>> glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=0))
            7  # "b" found at index 7 within the specified range, treating end=0 as the entire array's end.
        >>> glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=-1))
            7  # "b" found at index 7, with end=-1 covering the full array to its last element.
        >>> glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=7))
            -1  # "b" not found within the range from index 4 to exclusive end at index 7.
    """
    args = ["JSON.ARRINDEX", key, path, value]

    if options:
        args.extend(options.to_args())

    return cast(TJsonResponse[int], client.custom_command(args))

arrinsert(client, key, path, index, values)

Inserts one or more values into the array at the specified path within the JSON document stored at key, before the given index.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

The path within the JSON document.

required
index int

The array index before which values are inserted.

required
values List[TEncodable]

The JSON values to be inserted into the array, in JSON formatted bytes or str. Json string values must be wrapped with single quotes. For example, to append "foo", pass '"foo"'.

required

Returns:

Type Description
TJsonResponse[int]

TJsonResponse[int]: For JSONPath (path starts with '$'): Returns a list of integer replies for every possible path, indicating the new length of the array, or None for JSON values matching the path that are not an array. If path does not exist, an empty array will be returned. For legacy path (path doesn't start with '$'): Returns an integer representing the new length of the array. If multiple paths are matched, returns the length of the first modified array. If path doesn't exist or the value at path is not an array, an error is raised. If the index is out of bounds, an error is raised. If key doesn't exist, an error is raised.

TJsonResponse[int]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"]]')
    'OK'
>>> glide_json.arrinsert(client, "doc", "$[*]", 0, ['"c"', '{"key": "value"}', "true", "null", '["bar"]'])
    [5, 6, 7]  # New lengths of arrays after insertion
>>> glide_json.get(client, "doc")
    b'[["c",{"key":"value"},true,null,["bar"]],["c",{"key":"value"},true,null,["bar"],"a"],["c",{"key":"value"},true,null,["bar"],"a","b"]]'
>>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"]]')
    'OK'
>>> glide_json.arrinsert(client, "doc", ".", 0, ['"c"'])
    4  # New length of the root array after insertion
>>> glide_json.get(client, "doc")
    b'["c",[],["a"],["a","b"]]'
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def arrinsert(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
    index: int,
    values: List[TEncodable],
) -> TJsonResponse[int]:
    """
    Inserts one or more values into the array at the specified `path` within the JSON document stored at `key`,
    before the given `index`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): The path within the JSON document.
        index (int): The array index before which values are inserted.
        values (List[TEncodable]): The JSON values to be inserted into the array, in JSON formatted bytes or str.
            Json string values must be wrapped with single quotes. For example, to append "foo", pass '"foo"'.

    Returns:
        TJsonResponse[int]:
            For JSONPath (`path` starts with '$'):
                Returns a list of integer replies for every possible path, indicating the new length of the array,
                or None for JSON values matching the path that are not an array.
                If `path` does not exist, an empty array will be returned.
            For legacy path (`path` doesn't start with '$'):
                Returns an integer representing the new length of the array.
                If multiple paths are matched, returns the length of the first modified array.
                If `path` doesn't exist or the value at `path` is not an array, an error is raised.
            If the index is out of bounds, an error is raised.
            If `key` doesn't exist, an error is raised.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"]]')
            'OK'
        >>> glide_json.arrinsert(client, "doc", "$[*]", 0, ['"c"', '{"key": "value"}', "true", "null", '["bar"]'])
            [5, 6, 7]  # New lengths of arrays after insertion
        >>> glide_json.get(client, "doc")
            b'[["c",{"key":"value"},true,null,["bar"]],["c",{"key":"value"},true,null,["bar"],"a"],["c",{"key":"value"},true,null,["bar"],"a","b"]]'

        >>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"]]')
            'OK'
        >>> glide_json.arrinsert(client, "doc", ".", 0, ['"c"'])
            4  # New length of the root array after insertion
        >>> glide_json.get(client, "doc")
            b'[\"c\",[],[\"a\"],[\"a\",\"b\"]]'
    """
    args = ["JSON.ARRINSERT", key, path, str(index)] + values
    return cast(TJsonResponse[int], client.custom_command(args))

arrlen(client, key, path=None)

Retrieves the length of the array at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. Defaults to None.

None

Returns:

Type Description
Optional[TJsonResponse[int]]

Optional[TJsonResponse[int]]: For JSONPath (path starts with $): Returns a list of integer replies for every possible path, indicating the length of the array, or None for JSON values matching the path that are not an array. If path doesn't exist, an empty array will be returned. For legacy path (path doesn't starts with $): Returns the length of the array at path. If multiple paths match, the length of the first array match is returned. If the JSON value at path is not a array or if path doesn't exist, an error is raised. If key doesn't exist, None is returned.

Optional[TJsonResponse[int]]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": [1, 2, 3], "b": {"a": [1, 2], "c": {"a": 42}}}')
    'OK'  # JSON is successfully set for doc
>>> glide_json.arrlen(client, "doc", "$")
    [None]  # No array at the root path.
>>> glide_json.arrlen(client, "doc", "$.a")
    [3]  # Retrieves the length of the array at path $.a.
>>> glide_json.arrlen(client, "doc", "$..a")
    [3, 2, None]  # Retrieves lengths of arrays found at all levels of the path `$..a`.
>>> glide_json.arrlen(client, "doc", "..a")
    3  # Legacy path retrieves the first array match at path `..a`.
>>> glide_json.arrlen(client, "non_existing_key", "$.a")
    None  # Returns None because the key does not exist.
>>> glide_json.set(client, "doc", "$", '[1, 2, 3, 4]')
    'OK'  # JSON is successfully set for doc
>>> glide_json.arrlen(client, "doc")
    4  # Retrieves lengths of array in root.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def arrlen(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> Optional[TJsonResponse[int]]:
    """
    Retrieves the length of the array at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document. Defaults to None.

    Returns:
        Optional[TJsonResponse[int]]:
            For JSONPath (`path` starts with `$`):
                Returns a list of integer replies for every possible path, indicating the length of the array,
                or None for JSON values matching the path that are not an array.
                If `path` doesn't exist, an empty array will be returned.
            For legacy path (`path` doesn't starts with `$`):
                Returns the length of the array at `path`.
                If multiple paths match, the length of the first array match is returned.
                If the JSON value at `path` is not a array or if `path` doesn't exist, an error is raised.
            If `key` doesn't exist, None is returned.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": [1, 2, 3], "b": {"a": [1, 2], "c": {"a": 42}}}')
            'OK'  # JSON is successfully set for doc
        >>> glide_json.arrlen(client, "doc", "$")
            [None]  # No array at the root path.
        >>> glide_json.arrlen(client, "doc", "$.a")
            [3]  # Retrieves the length of the array at path $.a.
        >>> glide_json.arrlen(client, "doc", "$..a")
            [3, 2, None]  # Retrieves lengths of arrays found at all levels of the path `$..a`.
        >>> glide_json.arrlen(client, "doc", "..a")
            3  # Legacy path retrieves the first array match at path `..a`.
        >>> glide_json.arrlen(client, "non_existing_key", "$.a")
            None  # Returns None because the key does not exist.

        >>> glide_json.set(client, "doc", "$", '[1, 2, 3, 4]')
            'OK'  # JSON is successfully set for doc
        >>> glide_json.arrlen(client, "doc")
            4  # Retrieves lengths of array in root.
    """
    args = ["JSON.ARRLEN", key]
    if path:
        args.append(path)
    return cast(
        Optional[TJsonResponse[int]],
        client.custom_command(args),
    )

arrpop(client, key, options=None)

Pops an element from the array located at the specified path within the JSON document stored at key. If options.index is provided, it pops the element at that index instead of the last element.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
options Optional[JsonArrPopOptions]

Options including the path and optional index. See JsonArrPopOptions. Default to None. If not specified, attempts to pop the last element from the root value if it's an array. If the root value is not an array, an error will be raised.

None

Returns:

Type Description
Optional[TJsonResponse[bytes]]

Optional[TJsonResponse[bytes]]: For JSONPath (options.path starts with $): Returns a list of bytes string replies for every possible path, representing the popped JSON values, or None for JSON values matching the path that are not an array or are an empty array. If options.path doesn't exist, an empty list will be returned. For legacy path (options.path doesn't starts with $): Returns a bytes string representing the popped JSON value, or None if the array at options.path is empty. If multiple paths match, the value from the first matching array that is not empty is returned. If the JSON value at options.path is not a array or if options.path doesn't exist, an error is raised. If key doesn't exist, an error is raised.

Optional[TJsonResponse[bytes]]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(
...     client,
...     "doc",
...     "$",
...     '{"a": [1, 2, true], "b": {"a": [3, 4, ["value", 3, false], 5], "c": {"a": 42}}}'
... )
    b'OK'
>>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path="$.a", index=1))
    [b'2']  # Pop second element from array at path $.a
>>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path="$..a"))
    [b'true', b'5', None]  # Pop last elements from all arrays matching path `$..a`
Using a legacy path (..) to pop the first matching array
>>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path="..a"))
    b"1"  # First match popped (from array at path ..a)
Even though only one value is returned from ..a, subsequent arrays are also affected
>>> glide_json.get(client, "doc", "$..a")
    b"[[], [3, 4], 42]"  # Remaining elements after pop show the changes
>>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b", "c"]]')
    b'OK'  # JSON is successfully set
>>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path=".", index=-1))
    b'["a","b","c"]'  # Pop last elements at path `.`
>>> glide_json.arrpop(client, "doc")
    b'["a"]'  # Pop last elements at path `.`
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def arrpop(
    client: TGlideClient,
    key: TEncodable,
    options: Optional[JsonArrPopOptions] = None,
) -> Optional[TJsonResponse[bytes]]:
    """
    Pops an element from the array located at the specified path within the JSON document stored at `key`.
    If `options.index` is provided, it pops the element at that index instead of the last element.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        options (Optional[JsonArrPopOptions]): Options including the path and optional index. See `JsonArrPopOptions`.
            Default to None.
            If not specified, attempts to pop the last element from the root value if it's an array.
            If the root value is not an array, an error will be raised.

    Returns:
        Optional[TJsonResponse[bytes]]:
            For JSONPath (`options.path` starts with `$`):
                Returns a list of bytes string replies for every possible path, representing the popped JSON values,
                or None for JSON values matching the path that are not an array or are an empty array.
                If `options.path` doesn't exist, an empty list will be returned.
            For legacy path (`options.path` doesn't starts with `$`):
                Returns a bytes string representing the popped JSON value, or None if the array at `options.path` is empty.
                If multiple paths match, the value from the first matching array that is not empty is returned.
                If the JSON value at `options.path` is not a array or if `options.path` doesn't exist, an error is raised.
            If `key` doesn't exist, an error is raised.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(
        ...     client,
        ...     "doc",
        ...     "$",
        ...     '{"a": [1, 2, true], "b": {"a": [3, 4, ["value", 3, false], 5], "c": {"a": 42}}}'
        ... )
            b'OK'
        >>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path="$.a", index=1))
            [b'2']  # Pop second element from array at path $.a
        >>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path="$..a"))
            [b'true', b'5', None]  # Pop last elements from all arrays matching path `$..a`

        #### Using a legacy path (..) to pop the first matching array
        >>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path="..a"))
            b"1"  # First match popped (from array at path ..a)

        #### Even though only one value is returned from `..a`, subsequent arrays are also affected
        >>> glide_json.get(client, "doc", "$..a")
            b"[[], [3, 4], 42]"  # Remaining elements after pop show the changes

        >>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b", "c"]]')
            b'OK'  # JSON is successfully set
        >>> glide_json.arrpop(client, "doc", JsonArrPopOptions(path=".", index=-1))
            b'["a","b","c"]'  # Pop last elements at path `.`
        >>> glide_json.arrpop(client, "doc")
            b'["a"]'  # Pop last elements at path `.`
    """
    args = ["JSON.ARRPOP", key]
    if options:
        args.extend(options.to_args())

    return cast(
        Optional[TJsonResponse[bytes]],
        client.custom_command(args),
    )

arrtrim(client, key, path, start, end)

Trims an array at the specified path within the JSON document stored at key so that it becomes a subarray [start, end], both inclusive. If start < 0, it is treated as 0. If end >= size (size of the array), it is treated as size-1. If start >= size or start > end, the array is emptied and 0 is returned.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

The path within the JSON document.

required
start int

The start index, inclusive.

required
end int

The end index, inclusive.

required

Returns:

Type Description
TJsonResponse[int]

TJsonResponse[int]: For JSONPath (path starts with '$'): Returns a list of integer replies for every possible path, indicating the new length of the array, or None for JSON values matching the path that are not an array. If a value is an empty array, its corresponding return value is 0. If path doesn't exist, an empty array will be returned. For legacy path (path doesn't starts with $): Returns an integer representing the new length of the array. If the array is empty, returns 0. If multiple paths match, the length of the first trimmed array match is returned. If path doesn't exist, or the value at path is not an array, an error is raised. If key doesn't exist, an error is raised.

TJsonResponse[int]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"], ["a", "b", "c"]]')
    'OK'
>>> glide_json.arrtrim(client, "doc", "$[*]", 0, 1)
    [0, 1, 2, 2]
>>> glide_json.get(client, "doc")
    b'[[],["a"],["a","b"],["a","b"]]'
>>> glide_json.set(client, "doc", "$", '{"children": ["John", "Jack", "Tom", "Bob", "Mike"]}')
    'OK'
>>> glide_json.arrtrim(client, "doc", ".children", 0, 1)
    2
>>> glide_json.get(client, "doc", ".children")
    b'["John","Jack"]'
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
def arrtrim(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
    start: int,
    end: int,
) -> TJsonResponse[int]:
    """
    Trims an array at the specified `path` within the JSON document stored at `key` so that it becomes a subarray [start, end],
    both inclusive.
    If `start` < 0, it is treated as 0.
    If `end` >= size (size of the array), it is treated as size-1.
    If `start` >= size or `start` > `end`, the array is emptied and 0 is returned.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): The path within the JSON document.
        start (int): The start index, inclusive.
        end (int): The end index, inclusive.

    Returns:
        TJsonResponse[int]:
            For JSONPath (`path` starts with '$'):
                Returns a list of integer replies for every possible path, indicating the new length of the array, or None for
                JSON values matching the path that are not an array.
                If a value is an empty array, its corresponding return value is 0.
                If `path` doesn't exist, an empty array will be returned.
            For legacy path (`path` doesn't starts with `$`):
                Returns an integer representing the new length of the array.
                If the array is empty, returns 0.
                If multiple paths match, the length of the first trimmed array match is returned.
                If `path` doesn't exist, or the value at `path` is not an array, an error is raised.
            If `key` doesn't exist, an error is raised.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"], ["a", "b", "c"]]')
            'OK'
        >>> glide_json.arrtrim(client, "doc", "$[*]", 0, 1)
            [0, 1, 2, 2]
        >>> glide_json.get(client, "doc")
            b'[[],[\"a\"],[\"a\",\"b\"],[\"a\",\"b\"]]'

        >>> glide_json.set(client, "doc", "$", '{"children": ["John", "Jack", "Tom", "Bob", "Mike"]}')
            'OK'
        >>> glide_json.arrtrim(client, "doc", ".children", 0, 1)
            2
        >>> glide_json.get(client, "doc", ".children")
            b'["John","Jack"]'
    """
    return cast(
        TJsonResponse[int],
        client.custom_command(["JSON.ARRTRIM", key, path, str(start), str(end)]),
    )

clear(client, key, path=None)

Clears arrays or objects at the specified JSON path in the document stored at key. Numeric values are set to 0, and boolean values are set to False, and string values are converted to empty strings.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[str]

The path within the JSON document. Default to None.

None

Returns:

Name Type Description
int int

The number of containers cleared, numeric values zeroed, and booleans toggled to false,

int

and string values converted to empty strings.

int

If path doesn't exist, or the value at path is already empty (e.g., an empty array, object, or string),

int

0 is returned.

int

If `key doesn't exist, an error is raised.

Examples:

>>> from glide import glide_json
>>> glide_json.set(
...     client,
...     "doc",
...     "$",
...     '{"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14, "nullVal": null}'
... )
    'OK'  # JSON document is successfully set.
>>> glide_json.clear(client, "doc", "$.*")
    6      # 6 values are cleared (arrays/objects/strings/numbers/booleans), but `null` remains as is.
>>> glide_json.get(client, "doc", "$")
    b'[{"obj":{},"arr":[],"str":"","bool":false,"int":0,"float":0.0,"nullVal":null}]'
>>> glide_json.clear(client, "doc", "$.*")
    0  # No further clearing needed since the containers are already empty and the values are defaults.
>>> glide_json.set(
...     client,
...     "doc",
...     "$",
...     (
...         '{"a": 1, '
...         '"b": {"a": [5, 6, 7], "b": {"a": true}}, '
...         '"c": {"a": "value", "b": {"a": 3.5}}, '
...         '"d": {"a": {"foo": "foo"}}, '
...         '"nullVal": null}'
...     )
... )
    'OK'
>>> glide_json.clear(client, "doc", "b.a[1:3]")
    2  # 2 elements (`6` and `7`) are cleared.
>>> glide_json.clear(client, "doc", "b.a[1:3]")
    0 # No elements cleared since specified slice has already been cleared.
>>> glide_json.get(client, "doc", "$..a")
    b'[1,[5,0,0],true,"value",3.5,{"foo":"foo"}]'
>>> glide_json.clear(client, "doc", "$..a")
    6  # All numeric, boolean, and string values across paths are cleared.
>>> glide_json.get(client, "doc", "$..a")
    b'[0,[],false,"",0.0,{}]'
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
def clear(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[str] = None,
) -> int:
    """
    Clears arrays or objects at the specified JSON path in the document stored at `key`.
    Numeric values are set to `0`, and boolean values are set to `False`, and string values are converted to empty strings.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[str]): The path within the JSON document. Default to None.

    Returns:
        int: The number of containers cleared, numeric values zeroed, and booleans toggled to `false`,
        and string values converted to empty strings.
        If `path` doesn't exist, or the value at `path` is already empty (e.g., an empty array, object, or string),
        0 is returned.
        If `key doesn't exist, an error is raised.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(
        ...     client,
        ...     "doc",
        ...     "$",
        ...     '{"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14, "nullVal": null}'
        ... )
            'OK'  # JSON document is successfully set.
        >>> glide_json.clear(client, "doc", "$.*")
            6      # 6 values are cleared (arrays/objects/strings/numbers/booleans), but `null` remains as is.
        >>> glide_json.get(client, "doc", "$")
            b'[{"obj":{},"arr":[],"str":"","bool":false,"int":0,"float":0.0,"nullVal":null}]'
        >>> glide_json.clear(client, "doc", "$.*")
            0  # No further clearing needed since the containers are already empty and the values are defaults.

        >>> glide_json.set(
        ...     client,
        ...     "doc",
        ...     "$",
        ...     (
        ...         '{"a": 1, '
        ...         '"b": {"a": [5, 6, 7], "b": {"a": true}}, '
        ...         '"c": {"a": "value", "b": {"a": 3.5}}, '
        ...         '"d": {"a": {"foo": "foo"}}, '
        ...         '"nullVal": null}'
        ...     )
        ... )
            'OK'
        >>> glide_json.clear(client, "doc", "b.a[1:3]")
            2  # 2 elements (`6` and `7`) are cleared.
        >>> glide_json.clear(client, "doc", "b.a[1:3]")
            0 # No elements cleared since specified slice has already been cleared.
        >>> glide_json.get(client, "doc", "$..a")
            b'[1,[5,0,0],true,"value",3.5,{"foo":"foo"}]'

        >>> glide_json.clear(client, "doc", "$..a")
            6  # All numeric, boolean, and string values across paths are cleared.
        >>> glide_json.get(client, "doc", "$..a")
            b'[0,[],false,"",0.0,{}]'
    """
    args = ["JSON.CLEAR", key]
    if path:
        args.append(path)

    return cast(int, client.custom_command(args))

debug_fields(client, key, path=None)

Returns the number of fields of the JSON value at the specified path within the JSON document stored at key. - Primitive Values: Each non-container JSON value (e.g., strings, numbers, booleans, and null) counts as one field. - Arrays and Objects:: Each item in an array and each key-value pair in an object is counted as one field. (Each top-level value counts as one field, regardless of it's type.) - Their nested values are counted recursively and added to the total. - Example: For the JSON {"a": 1, "b": [2, 3, {"c": 4}]}, the count would be: - Top-level: 2 fields ("a" and "b") - Nested: 3 fields in the array (2, 3, and {"c": 4}) plus 1 for the object ("c") - Total: 2 (top-level) + 3 (from array) + 1 (from nested object) = 6 fields.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. Defaults to root if not provided.

None

Returns:

Type Description
Optional[TJsonUniversalResponse[int]]

Optional[TJsonUniversalResponse[int]]: For JSONPath (path starts with $): Returns an array of integers, each indicating the number of fields for each matched path. If path doesn't exist, an empty array will be returned. For legacy path (path doesn't start with $): Returns an integer indicating the number of fields for each matched path. If multiple paths match, number of fields of the first JSON value match is returned. If path doesn't exist, an error is raised. If path is not provided, it reports the total number of fields in the entire JSON document. If key doesn't exist, None is returned.

Optional[TJsonUniversalResponse[int]]

For more information about the returned type, see TJsonUniversalResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "k1", "$", '[1, 2.3, "foo", true, null, {}, [], {"a":1, "b":2}, [1,2,3]]')
    'OK'
>>> glide_json.debug_fields(client, "k1", "$[*]")
    [1, 1, 1, 1, 1, 0, 0, 2, 3]
>>> glide_json.debug_fields(client, "k1", ".")
    14 # 9 top-level fields + 5 nested address fields
>>> glide_json.set(
...     client,
...     "k1",
...     "$",
...     (
...         '{"firstName":"John", '
...         '"lastName":"Smith", '
...         '"age":27, '
...         '"weight":135.25, '
...         '"isAlive":true, '
...         '"address":{"street":"21 2nd Street","city":"New York","state":"NY","zipcode":"10021-3100"}, '
...         '"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}], '
...         '"children":[], '
...         '"spouse":null}'
...     )
... )
    'OK'
>>> glide_json.debug_fields(client, "k1")
    19
>>> glide_json.debug_fields(client, "k1", ".address")
    4
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
def debug_fields(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> Optional[TJsonUniversalResponse[int]]:
    """
    Returns the number of fields of the JSON value at the specified `path` within the JSON document stored at `key`.
    - **Primitive Values**: Each non-container JSON value (e.g., strings, numbers, booleans, and null) counts as one field.
    - **Arrays and Objects:**: Each item in an array and each key-value pair in an object is counted as one field.
        (Each top-level value counts as one field, regardless of it's type.)
        - Their nested values are counted recursively and added to the total.
        - **Example**: For the JSON `{"a": 1, "b": [2, 3, {"c": 4}]}`, the count would be:
            - Top-level: 2 fields (`"a"` and `"b"`)
            - Nested: 3 fields in the array (`2`, `3`, and `{"c": 4}`) plus 1 for the object (`"c"`)
            - Total: 2 (top-level) + 3 (from array) + 1 (from nested object) = 6 fields.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document. Defaults to root if not provided.

    Returns:
        Optional[TJsonUniversalResponse[int]]:
            For JSONPath (`path` starts with `$`):
                Returns an array of integers, each indicating the number of fields for each matched `path`.
                If `path` doesn't exist, an empty array will be returned.
            For legacy path (`path` doesn't start with `$`):
                Returns an integer indicating the number of fields for each matched `path`.
                If multiple paths match, number of fields of the first JSON value match is returned.
                If `path` doesn't exist, an error is raised.
            If `path` is not provided, it reports the total number of fields in the entire JSON document.
            If `key` doesn't exist, None is returned.
        For more information about the returned type, see `TJsonUniversalResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "k1", "$", '[1, 2.3, "foo", true, null, {}, [], {"a":1, "b":2}, [1,2,3]]')
            'OK'
        >>> glide_json.debug_fields(client, "k1", "$[*]")
            [1, 1, 1, 1, 1, 0, 0, 2, 3]
        >>> glide_json.debug_fields(client, "k1", ".")
            14 # 9 top-level fields + 5 nested address fields

        >>> glide_json.set(
        ...     client,
        ...     "k1",
        ...     "$",
        ...     (
        ...         '{"firstName":"John", '
        ...         '"lastName":"Smith", '
        ...         '"age":27, '
        ...         '"weight":135.25, '
        ...         '"isAlive":true, '
        ...         '"address":{"street":"21 2nd Street","city":"New York","state":"NY","zipcode":"10021-3100"}, '
        ...         '"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}], '
        ...         '"children":[], '
        ...         '"spouse":null}'
        ...     )
        ... )
            'OK'
        >>> glide_json.debug_fields(client, "k1")
            19
        >>> glide_json.debug_fields(client, "k1", ".address")
            4
    """
    args = ["JSON.DEBUG", "FIELDS", key]
    if path:
        args.append(path)

    return cast(Optional[TJsonUniversalResponse[int]], client.custom_command(args))

debug_memory(client, key, path=None)

Reports memory usage in bytes of a JSON value at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. Defaults to None.

None

Returns:

Type Description
Optional[TJsonUniversalResponse[int]]

Optional[TJsonUniversalResponse[int]]: For JSONPath (path starts with $): Returns an array of integers, indicating the memory usage in bytes of a JSON value for each matched path. If path doesn't exist, an empty array will be returned. For legacy path (path doesn't start with $): Returns an integer, indicating the memory usage in bytes for the JSON value in path. If multiple paths match, the memory usage of the first JSON value match is returned. If path doesn't exist, an error is raised. If path is not provided, it reports the total memory usage in bytes in the entire JSON document. If key doesn't exist, None is returned.

Optional[TJsonUniversalResponse[int]]

For more information about the returned type, see TJsonUniversalResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "k1", "$", '[1, 2.3, "foo", true, null, {}, [], {"a":1, "b":2}, [1,2,3]]')
    'OK'
>>> glide_json.debug_memory(client, "k1", "$[*]")
    [16, 16, 19, 16, 16, 16, 16, 66, 64]
>>> glide_json.set(
...     client,
...     "k1",
...     "$",
...     (
...         '{"firstName":"John", '
...         '"lastName":"Smith", '
...         '"age":27, '
...         '"weight":135.25, '
...         '"isAlive":true, '
...         '"address":{"street":"21 2nd Street","city":"New York","state":"NY","zipcode":"10021-3100"}, '
...         '"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}], '
...         '"children":[], '
...         '"spouse":null}'
...     )
... )
    'OK'
>>> glide_json.debug_memory(client, "k1")
    472
>>> glide_json.debug_memory(client, "k1", ".phoneNumbers")
    164
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
def debug_memory(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> Optional[TJsonUniversalResponse[int]]:
    """
    Reports memory usage in bytes of a JSON value at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document. Defaults to None.

    Returns:
        Optional[TJsonUniversalResponse[int]]:
            For JSONPath (`path` starts with `$`):
                Returns an array of integers, indicating the memory usage in bytes of a JSON value for each matched `path`.
                If `path` doesn't exist, an empty array will be returned.
            For legacy path (`path` doesn't start with `$`):
                Returns an integer, indicating the memory usage in bytes for the JSON value in `path`.
                If multiple paths match, the memory usage of the first JSON value match is returned.
                If `path` doesn't exist, an error is raised.
            If `path` is not provided, it reports the total memory usage in bytes in the entire JSON document.
            If `key` doesn't exist, None is returned.
        For more information about the returned type, see `TJsonUniversalResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "k1", "$", '[1, 2.3, "foo", true, null, {}, [], {"a":1, "b":2}, [1,2,3]]')
            'OK'
        >>> glide_json.debug_memory(client, "k1", "$[*]")
            [16, 16, 19, 16, 16, 16, 16, 66, 64]

        >>> glide_json.set(
        ...     client,
        ...     "k1",
        ...     "$",
        ...     (
        ...         '{"firstName":"John", '
        ...         '"lastName":"Smith", '
        ...         '"age":27, '
        ...         '"weight":135.25, '
        ...         '"isAlive":true, '
        ...         '"address":{"street":"21 2nd Street","city":"New York","state":"NY","zipcode":"10021-3100"}, '
        ...         '"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}], '
        ...         '"children":[], '
        ...         '"spouse":null}'
        ...     )
        ... )
            'OK'
        >>> glide_json.debug_memory(client, "k1")
            472
        >>> glide_json.debug_memory(client, "k1", ".phoneNumbers")
            164
    """
    args = ["JSON.DEBUG", "MEMORY", key]
    if path:
        args.append(path)

    return cast(Optional[TJsonUniversalResponse[int]], client.custom_command(args))

delete(client, key, path=None)

Deletes the JSON value at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. If None, deletes the entire JSON document at key. Defaults to None.

None

Returns:

Name Type Description
int int

The number of elements removed.

int

If key or path doesn't exist, returns 0.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
    'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
>>> glide_json.delete(client, "doc", "$..a")
    2  # Indicates successful deletion of the specific values in the key stored at `doc`.
>>> glide_json.get(client, "doc", "$")
    "[{"nested":{"b":3}}]"  # Returns the value at path '$' in the JSON document stored at `doc`.
>>> glide_json.delete(client, "doc")
    1  # Deletes the entire JSON document stored at `doc`.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
def delete(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> int:
    """
    Deletes the JSON value at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document.
            If None, deletes the entire JSON document at `key`. Defaults to None.

    Returns:
        int: The number of elements removed.
        If `key` or `path` doesn't exist, returns 0.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
            'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
        >>> glide_json.delete(client, "doc", "$..a")
            2  # Indicates successful deletion of the specific values in the key stored at `doc`.
        >>> glide_json.get(client, "doc", "$")
            "[{\"nested\":{\"b\":3}}]"  # Returns the value at path '$' in the JSON document stored at `doc`.
        >>> glide_json.delete(client, "doc")
            1  # Deletes the entire JSON document stored at `doc`.
    """

    return cast(
        int, client.custom_command(["JSON.DEL", key] + ([path] if path else []))
    )

forget(client, key, path=None)

Deletes the JSON value at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. If None, deletes the entire JSON document at key. Defaults to None.

None

Returns:

Name Type Description
int Optional[int]

The number of elements removed.

Optional[int]

If key or path doesn't exist, returns 0.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
    'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
>>> glide_json.forget(client, "doc", "$..a")
    2  # Indicates successful deletion of the specific values in the key stored at `doc`.
>>> glide_json.get(client, "doc", "$")
    "[{"nested":{"b":3}}]"  # Returns the value at path '$' in the JSON document stored at `doc`.
>>> glide_json.forget(client, "doc")
    1  # Deletes the entire JSON document stored at `doc`.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
def forget(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> Optional[int]:
    """
    Deletes the JSON value at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document.
            If None, deletes the entire JSON document at `key`. Defaults to None.

    Returns:
        int: The number of elements removed.
        If `key` or `path` doesn't exist, returns 0.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
            'OK'  # Indicates successful setting of the value at path '$' in the key stored at `doc`.
        >>> glide_json.forget(client, "doc", "$..a")
            2  # Indicates successful deletion of the specific values in the key stored at `doc`.
        >>> glide_json.get(client, "doc", "$")
            "[{\"nested\":{\"b\":3}}]"  # Returns the value at path '$' in the JSON document stored at `doc`.
        >>> glide_json.forget(client, "doc")
            1  # Deletes the entire JSON document stored at `doc`.
    """

    return cast(
        Optional[int],
        client.custom_command(["JSON.FORGET", key] + ([path] if path else [])),
    )

mget(client, keys, path)

Retrieves the JSON values at the specified path stored at multiple keys.

Note

In cluster mode, if keys in keys map to different hash slots, the command will be split across these slots and executed separately for each. This means the command is atomic only at the slot level. If one or more slot-specific requests fail, the entire call will return the first encountered error, even though some requests may have succeeded while others did not. If this behavior impacts your application logic, consider splitting the request into sub-requests per slot to ensure atomicity.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
keys List[TEncodable]

A list of keys for the JSON documents.

required
path TEncodable

The path within the JSON documents.

required

Returns:

Type Description
List[Optional[bytes]]

List[Optional[bytes]]: For JSONPath (path starts with $): Returns a list of byte representations of the values found at the given path for each key. If path does not exist within the key, the entry will be an empty array. For legacy path (path doesn't starts with $): Returns a list of byte representations of the values found at the given path for each key. If path does not exist within the key, the entry will be None. If a key doesn't exist, the corresponding list element will be None.

Examples:

>>> from glide import glide_json
>>> import json
>>> json_strs = glide_json.mget(client, ["doc1", "doc2"], "$")
>>> [json.loads(js) for js in json_strs]  # Parse JSON strings to Python data
    [[{"a": 1.0, "b": 2}], [{"a": 2.0, "b": {"a": 3.0, "b" : 4.0}}]]  # JSON objects retrieved from keys
                                                                      # `doc1` and `doc2`
>>> glide_json.mget(client, ["doc1", "doc2"], "$.a")
    [b"[1.0]", b"[2.0]"]  # Returns values at path '$.a' for the JSON documents stored at `doc1` and `doc2`.
>>> glide_json.mget(client, ["doc1"], "$.non_existing_path")
    [None]  # Returns an empty array since the path '$.non_existing_path' does not exist in the JSON document
            # stored at `doc1`.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
def mget(
    client: TGlideClient,
    keys: List[TEncodable],
    path: TEncodable,
) -> List[Optional[bytes]]:
    """
    Retrieves the JSON values at the specified `path` stored at multiple `keys`.

    Note:
        In cluster mode, if keys in `keys` map to different hash slots, the command
        will be split across these slots and executed separately for each. This means the command
        is atomic only at the slot level. If one or more slot-specific requests fail, the entire
        call will return the first encountered error, even though some requests may have succeeded
        while others did not. If this behavior impacts your application logic, consider splitting
        the request into sub-requests per slot to ensure atomicity.

    Args:
        client (TGlideClient): The client to execute the command.
        keys (List[TEncodable]): A list of keys for the JSON documents.
        path (TEncodable): The path within the JSON documents.

    Returns:
        List[Optional[bytes]]:
            For JSONPath (`path` starts with `$`):
                Returns a list of byte representations of the values found at the given path for each key.
                If `path` does not exist within the key, the entry will be an empty array.
            For legacy path (`path` doesn't starts with `$`):
                Returns a list of byte representations of the values found at the given path for each key.
                If `path` does not exist within the key, the entry will be None.
            If a key doesn't exist, the corresponding list element will be None.


    Examples:
        >>> from glide import glide_json
        >>> import json
        >>> json_strs = glide_json.mget(client, ["doc1", "doc2"], "$")
        >>> [json.loads(js) for js in json_strs]  # Parse JSON strings to Python data
            [[{"a": 1.0, "b": 2}], [{"a": 2.0, "b": {"a": 3.0, "b" : 4.0}}]]  # JSON objects retrieved from keys
                                                                              # `doc1` and `doc2`
        >>> glide_json.mget(client, ["doc1", "doc2"], "$.a")
            [b"[1.0]", b"[2.0]"]  # Returns values at path '$.a' for the JSON documents stored at `doc1` and `doc2`.
        >>> glide_json.mget(client, ["doc1"], "$.non_existing_path")
            [None]  # Returns an empty array since the path '$.non_existing_path' does not exist in the JSON document
                    # stored at `doc1`.
    """
    args = ["JSON.MGET"] + keys + [path]
    return cast(List[Optional[bytes]], client.custom_command(args))

numincrby(client, key, path, number)

Increments or decrements the JSON value(s) at the specified path by number within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

The path within the JSON document.

required
number Union[int, float]

The number to increment or decrement by.

required

Returns:

Name Type Description
bytes bytes

For JSONPath (path starts with $): Returns a bytes string representation of an array of bulk strings, indicating the new values after incrementing for each matched path. If a value is not a number, its corresponding return value will be null. If path doesn't exist, a byte string representation of an empty array will be returned. For legacy path (path doesn't start with $): Returns a bytes string representation of the resulting value after the increment or decrement. If multiple paths match, the result of the last updated value is returned. If the value at the path is not a number or path doesn't exist, an error is raised. If key does not exist, an error is raised. If the result is out of the range of 64-bit IEEE double, an error is raised.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": [], "b": [1], "c": [1, 2], "d": [1, 2, 3]}')
    'OK'
>>> glide_json.numincrby(client, "doc", "$.d[*]", 10)
    b'[11,12,13]'  # Increment each element in `d` array by 10.
>>> glide_json.numincrby(client, "doc", ".c[1]", 10)
    b'12'  # Increment the second element in the `c` array by 10.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
def numincrby(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
    number: Union[int, float],
) -> bytes:
    """
    Increments or decrements the JSON value(s) at the specified `path` by `number` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): The path within the JSON document.
        number (Union[int, float]): The number to increment or decrement by.

    Returns:
        bytes:
            For JSONPath (`path` starts with `$`):
                Returns a bytes string representation of an array of bulk strings, indicating the new values after
                incrementing for each matched `path`.
                If a value is not a number, its corresponding return value will be `null`.
                If `path` doesn't exist, a byte string representation of an empty array will be returned.
            For legacy path (`path` doesn't start with `$`):
                Returns a bytes string representation of the resulting value after the increment or decrement.
                If multiple paths match, the result of the last updated value is returned.
                If the value at the `path` is not a number or `path` doesn't exist, an error is raised.
            If `key` does not exist, an error is raised.
            If the result is out of the range of 64-bit IEEE double, an error is raised.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": [], "b": [1], "c": [1, 2], "d": [1, 2, 3]}')
            'OK'
        >>> glide_json.numincrby(client, "doc", "$.d[*]", 10)
            b'[11,12,13]'  # Increment each element in `d` array by 10.
        >>> glide_json.numincrby(client, "doc", ".c[1]", 10)
            b'12'  # Increment the second element in the `c` array by 10.
    """
    args = ["JSON.NUMINCRBY", key, path, str(number)]

    return cast(bytes, client.custom_command(args))

nummultby(client, key, path, number)

Multiplies the JSON value(s) at the specified path by number within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

The path within the JSON document.

required
number Union[int, float]

The number to multiply by.

required

Returns:

Name Type Description
bytes bytes

For JSONPath (path starts with $): Returns a bytes string representation of an array of bulk strings, indicating the new values after multiplication for each matched path. If a value is not a number, its corresponding return value will be null. If path doesn't exist, a byte string representation of an empty array will be returned. For legacy path (path doesn't start with $): Returns a bytes string representation of the resulting value after multiplication. If multiple paths match, the result of the last updated value is returned. If the value at the path is not a number or path doesn't exist, an error is raised. If key does not exist, an error is raised. If the result is out of the range of 64-bit IEEE double, an error is raised.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": [], "b": [1], "c": [1, 2], "d": [1, 2, 3]}')
    'OK'
>>> glide_json.nummultby(client, "doc", "$.d[*]", 2)
    b'[2,4,6]'  # Multiplies each element in the `d` array by 2.
>>> glide_json.nummultby(client, "doc", ".c[1]", 2)
    b'4'  # Multiplies the second element in the `c` array by 2.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
def nummultby(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
    number: Union[int, float],
) -> bytes:
    """
    Multiplies the JSON value(s) at the specified `path` by `number` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): The path within the JSON document.
        number (Union[int, float]): The number to multiply by.

    Returns:
        bytes:
            For JSONPath (`path` starts with `$`):
                Returns a bytes string representation of an array of bulk strings, indicating the new values after
                multiplication for each matched `path`.
                If a value is not a number, its corresponding return value will be `null`.
                If `path` doesn't exist, a byte string representation of an empty array will be returned.
            For legacy path (`path` doesn't start with `$`):
                Returns a bytes string representation of the resulting value after multiplication.
                If multiple paths match, the result of the last updated value is returned.
                If the value at the `path` is not a number or `path` doesn't exist, an error is raised.
            If `key` does not exist, an error is raised.
            If the result is out of the range of 64-bit IEEE double, an error is raised.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": [], "b": [1], "c": [1, 2], "d": [1, 2, 3]}')
            'OK'
        >>> glide_json.nummultby(client, "doc", "$.d[*]", 2)
            b'[2,4,6]'  # Multiplies each element in the `d` array by 2.
        >>> glide_json.nummultby(client, "doc", ".c[1]", 2)
            b'4'  # Multiplies the second element in the `c` array by 2.
    """
    args = ["JSON.NUMMULTBY", key, path, str(number)]

    return cast(bytes, client.custom_command(args))

objlen(client, key, path=None)

Retrieves the number of key-value pairs in the object stored at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. Defaults to None.

None

Returns:

Type Description
Optional[TJsonResponse[int]]

Optional[TJsonResponse[int]]: For JSONPath (path starts with $): Returns a list of integer replies for every possible path, indicating the length of the object, or None for JSON values matching the path that are not an object. If path doesn't exist, an empty array will be returned. For legacy path (path doesn't starts with $): Returns the length of the object at path. If multiple paths match, the length of the first object match is returned. If the JSON value at path is not an object or if path doesn't exist, an error is raised. If key doesn't exist, None is returned.

Optional[TJsonResponse[int]]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": 1.0, "b": {"a": {"x": 1, "y": 2}, "b": 2.5, "c": true}}')
    b'OK'  # Indicates successful setting of the value at the root path '$' in the key `doc`.
>>> glide_json.objlen(client, "doc", "$")
    [2]  # Returns the number of key-value pairs at the root object, which has 2 keys: 'a' and 'b'.
>>> glide_json.objlen(client, "doc", ".")
    2  # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
>>> glide_json.objlen(client, "doc", "$.b")
    [3]  # Returns the length of the object at path '$.b', which has 3 keys: 'a', 'b', and 'c'.
>>> glide_json.objlen(client, "doc", ".b")
    3  # Returns the length of the nested object at path '.b', which has 3 keys.
>>> glide_json.objlen(client, "doc", "$..a")
    [None, 2]
>>> glide_json.objlen(client, "doc")
    2  # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
def objlen(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> Optional[TJsonResponse[int]]:
    """
    Retrieves the number of key-value pairs in the object stored at the specified `path` within the JSON document stored at
    `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document. Defaults to None.

    Returns:
        Optional[TJsonResponse[int]]:
            For JSONPath (`path` starts with `$`):
                Returns a list of integer replies for every possible path, indicating the length of the object,
                or None for JSON values matching the path that are not an object.
                If `path` doesn't exist, an empty array will be returned.
            For legacy path (`path` doesn't starts with `$`):
                Returns the length of the object at `path`.
                If multiple paths match, the length of the first object match is returned.
                If the JSON value at `path` is not an object or if `path` doesn't exist, an error is raised.
            If `key` doesn't exist, None is returned.
        For more information about the returned type, see `TJsonResponse`.


    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": 1.0, "b": {"a": {"x": 1, "y": 2}, "b": 2.5, "c": true}}')
            b'OK'  # Indicates successful setting of the value at the root path '$' in the key `doc`.
        >>> glide_json.objlen(client, "doc", "$")
            [2]  # Returns the number of key-value pairs at the root object, which has 2 keys: 'a' and 'b'.
        >>> glide_json.objlen(client, "doc", ".")
            2  # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
        >>> glide_json.objlen(client, "doc", "$.b")
            [3]  # Returns the length of the object at path '$.b', which has 3 keys: 'a', 'b', and 'c'.
        >>> glide_json.objlen(client, "doc", ".b")
            3  # Returns the length of the nested object at path '.b', which has 3 keys.
        >>> glide_json.objlen(client, "doc", "$..a")
            [None, 2]
        >>> glide_json.objlen(client, "doc")
            2  # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
    """
    args = ["JSON.OBJLEN", key]
    if path:
        args.append(path)
    return cast(
        Optional[TJsonResponse[int]],
        client.custom_command(args),
    )

objkeys(client, key, path=None)

Retrieves key names in the object values at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

Represents the path within the JSON document where the key names will be retrieved. Defaults to None.

None

Returns:

Type Description
Optional[TJsonUniversalResponse[List[bytes]]]

Optional[TJsonUniversalResponse[List[bytes]]]: For JSONPath (path starts with $): Returns a list of arrays containing key names for each matching object. If a value matching the path is not an object, an empty array is returned. If path doesn't exist, an empty array is returned. For legacy path (path starts with .): Returns a list of key names for the object value matching the path. If multiple objects match the path, the key names of the first object are returned. If a value matching the path is not an object, an error is raised. If path doesn't exist, None is returned. If key doesn't exist, None is returned.

Optional[TJsonUniversalResponse[List[bytes]]]

For more information about the returned type, see TJsonUniversalResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": 1.0, "b": {"a": {"x": 1, "y": 2}, "b": 2.5, "c": true}}')
    b'OK'  # Indicates successful setting of the value at the root path '$' in the key `doc`.
>>> glide_json.objkeys(client, "doc", "$")
    [[b"a", b"b"]]  # Returns a list of arrays containing the key names for objects matching the path '$'.
>>> glide_json.objkeys(client, "doc", ".")
    [b"a", b"b"]  # Returns key names for the object matching the path '.' as it is the only match.
>>> glide_json.objkeys(client, "doc", "$.b")
    [[b"a", b"b", b"c"]]  # Returns key names as a nested list for objects matching the JSONPath '$.b'.
>>> glide_json.objkeys(client, "doc", ".b")
    [b"a", b"b", b"c"]  # Returns key names for the nested object at path '.b'.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
def objkeys(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> Optional[TJsonUniversalResponse[List[bytes]]]:
    """
    Retrieves key names in the object values at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): Represents the path within the JSON document where the key names will be retrieved.
            Defaults to None.

    Returns:
        Optional[TJsonUniversalResponse[List[bytes]]]:
            For JSONPath (`path` starts with `$`):
                Returns a list of arrays containing key names for each matching object.
                If a value matching the path is not an object, an empty array is returned.
                If `path` doesn't exist, an empty array is returned.
            For legacy path (`path` starts with `.`):
                Returns a list of key names for the object value matching the path.
                If multiple objects match the path, the key names of the first object are returned.
                If a value matching the path is not an object, an error is raised.
                If `path` doesn't exist, None is returned.
            If `key` doesn't exist, None is returned.
        For more information about the returned type, see `TJsonUniversalResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": 1.0, "b": {"a": {"x": 1, "y": 2}, "b": 2.5, "c": true}}')
            b'OK'  # Indicates successful setting of the value at the root path '$' in the key `doc`.
        >>> glide_json.objkeys(client, "doc", "$")
            [[b"a", b"b"]]  # Returns a list of arrays containing the key names for objects matching the path '$'.
        >>> glide_json.objkeys(client, "doc", ".")
            [b"a", b"b"]  # Returns key names for the object matching the path '.' as it is the only match.
        >>> glide_json.objkeys(client, "doc", "$.b")
            [[b"a", b"b", b"c"]]  # Returns key names as a nested list for objects matching the JSONPath '$.b'.
        >>> glide_json.objkeys(client, "doc", ".b")
            [b"a", b"b", b"c"]  # Returns key names for the nested object at path '.b'.
    """
    args = ["JSON.OBJKEYS", key]
    if path:
        args.append(path)
    return cast(
        Optional[Union[List[bytes], List[List[bytes]]]],
        client.custom_command(args),
    )

resp(client, key, path=None)

Retrieve the JSON value at the specified path within the JSON document stored at key. The returning result is in the Valkey or Redis OSS Serialization Protocol (RESP).

JSON null is mapped to the RESP Null Bulk String.

JSON Booleans are mapped to RESP Simple string.

JSON integers are mapped to RESP Integers.

JSON doubles are mapped to RESP Bulk Strings.

JSON strings are mapped to RESP Bulk Strings.

JSON arrays are represented as RESP arrays, where the first element is the simple string [, followed by the array's elements.

JSON objects are represented as RESP object, where the first element is the simple string {, followed by key-value pairs, each of which is a RESP bulk string.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. Default to None.

None

Returns:

Type Description
TJsonUniversalResponse[Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]]

TJsonUniversalResponse[Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]] For JSONPath ('path' starts with '$'): Returns a list of replies for every possible path, indicating the RESP form of the JSON value. If path doesn't exist, returns an empty list. For legacy path (path doesn't starts with $): Returns a single reply for the JSON value at the specified path, in its RESP form. This can be a bytes object, an integer, None, or a list representing complex structures. If multiple paths match, the value of the first JSON value match is returned. If path doesn't exist, an error is raised. If key doesn't exist, an None is returned.

TJsonUniversalResponse[Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]]

For more information about the returned type, see TJsonUniversalResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": [1, 2, 3], "b": {"a": [1, 2], "c": {"a": 42}}}')
    'OK'
>>> glide_json.resp(client, "doc", "$..a")
    [[b"[", 1, 2, 3],[b"[", 1, 2],42]
>>> glide_json.resp(client, "doc", "..a")
    [b"[", 1, 2, 3]
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
def resp(
    client: TGlideClient, key: TEncodable, path: Optional[TEncodable] = None
) -> TJsonUniversalResponse[
    Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]
]:
    """
    Retrieve the JSON value at the specified `path` within the JSON document stored at `key`.
    The returning result is in the Valkey or Redis OSS Serialization Protocol (RESP).\n
    JSON null is mapped to the RESP Null Bulk String.\n
    JSON Booleans are mapped to RESP Simple string.\n
    JSON integers are mapped to RESP Integers.\n
    JSON doubles are mapped to RESP Bulk Strings.\n
    JSON strings are mapped to RESP Bulk Strings.\n
    JSON arrays are represented as RESP arrays, where the first element is the simple string [, followed by the array's
    elements.\n
    JSON objects are represented as RESP object, where the first element is the simple string {, followed by key-value pairs,
    each of which is a RESP bulk string.\n


    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document. Default to None.

    Returns:
        TJsonUniversalResponse[Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]]
            For JSONPath ('path' starts with '$'):
                Returns a list of replies for every possible path, indicating the RESP form of the JSON value.
                If `path` doesn't exist, returns an empty list.
            For legacy path (`path` doesn't starts with `$`):
                Returns a single reply for the JSON value at the specified path, in its RESP form.
                This can be a bytes object, an integer, None, or a list representing complex structures.
                If multiple paths match, the value of the first JSON value match is returned.
                If `path` doesn't exist, an error is raised.
            If `key` doesn't exist, an None is returned.
        For more information about the returned type, see `TJsonUniversalResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": [1, 2, 3], "b": {"a": [1, 2], "c": {"a": 42}}}')
            'OK'
        >>> glide_json.resp(client, "doc", "$..a")
            [[b"[", 1, 2, 3],[b"[", 1, 2],42]
        >>> glide_json.resp(client, "doc", "..a")
            [b"[", 1, 2, 3]
    """
    args = ["JSON.RESP", key]
    if path:
        args.append(path)

    return cast(
        TJsonUniversalResponse[
            Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]
        ],
        client.custom_command(args),
    )

strappend(client, key, value, path=None)

Appends the specified value to the string stored at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
value TEncodable

The value to append to the string. Must be wrapped with single quotes. For example, to append "foo", pass '"foo"'.

required
path Optional[TEncodable]

The path within the JSON document. Default to None.

None

Returns:

Type Description
TJsonResponse[int]

TJsonResponse[int]: For JSONPath (path starts with $): Returns a list of integer replies for every possible path, indicating the length of the resulting string after appending value, or None for JSON values matching the path that are not string. If key doesn't exist, an error is raised. For legacy path (path doesn't start with $): Returns the length of the resulting string after appending value to the string at path. If multiple paths match, the length of the last updated string is returned. If the JSON value at path is not a string of if path doesn't exist, an error is raised. If key doesn't exist, an error is raised.

TJsonResponse[int]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> import json
>>> glide_json.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
    'OK'
>>> glide_json.strappend(client, "doc", json.dumps("baz"), "$..a")
    [6, 8, None]  # The new length of the string values at path '$..a' in the key stored at `doc` after the append
                  # operation.
>>> glide_json.strappend(client, "doc", '"foo"', "nested.a")
    11  # The length of the string value after appending "foo" to the string at path 'nested.array' in the key stored
        # at `doc`.
>>> json.loads(glide_json.get(client, json.dumps("doc"), "$"))
    [{"a":"foobaz", "nested": {"a": "hellobazfoo"}, "nested2": {"a": 31}}] # The updated JSON value in the key stored
                                                                           # at `doc`.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
def strappend(
    client: TGlideClient,
    key: TEncodable,
    value: TEncodable,
    path: Optional[TEncodable] = None,
) -> TJsonResponse[int]:
    """
    Appends the specified `value` to the string stored at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        value (TEncodable): The value to append to the string. Must be wrapped with single quotes. For example,
            to append "foo", pass '"foo"'.
        path (Optional[TEncodable]): The path within the JSON document. Default to None.

    Returns:
        TJsonResponse[int]:
            For JSONPath (`path` starts with `$`):
                Returns a list of integer replies for every possible path, indicating the length of the resulting string after
                appending `value`,
                or None for JSON values matching the path that are not string.
                If `key` doesn't exist, an error is raised.
            For legacy path (`path` doesn't start with `$`):
                Returns the length of the resulting string after appending `value` to the string at `path`.
                If multiple paths match, the length of the last updated string is returned.
                If the JSON value at `path` is not a string of if `path` doesn't exist, an error is raised.
                If `key` doesn't exist, an error is raised.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> import json
        >>> glide_json.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
            'OK'
        >>> glide_json.strappend(client, "doc", json.dumps("baz"), "$..a")
            [6, 8, None]  # The new length of the string values at path '$..a' in the key stored at `doc` after the append
                          # operation.
        >>> glide_json.strappend(client, "doc", '"foo"', "nested.a")
            11  # The length of the string value after appending "foo" to the string at path 'nested.array' in the key stored
                # at `doc`.
        >>> json.loads(glide_json.get(client, json.dumps("doc"), "$"))
            [{"a":"foobaz", "nested": {"a": "hellobazfoo"}, "nested2": {"a": 31}}] # The updated JSON value in the key stored
                                                                                   # at `doc`.
    """

    return cast(
        TJsonResponse[int],
        client.custom_command(
            ["JSON.STRAPPEND", key] + ([path, value] if path else [value])
        ),
    )

strlen(client, key, path=None)

Returns the length of the JSON string value stored at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. Default to None.

None

Returns:

Type Description
TJsonResponse[Optional[int]]

TJsonResponse[Optional[int]]: For JSONPath (path starts with $): Returns a list of integer replies for every possible path, indicating the length of the JSON string value, or None for JSON values matching the path that are not string. For legacy path (path doesn't start with $): Returns the length of the JSON value at path or None if key doesn't exist. If multiple paths match, the length of the first mached string is returned. If the JSON value at path is not a string of if path doesn't exist, an error is raised. If key doesn't exist, None is returned.

TJsonResponse[Optional[int]]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> import json
>>> glide_json.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
    'OK'
>>> glide_json.strlen(client, "doc", "$..a")
    [3, 5, None]  # The length of the string values at path '$..a' in the key stored at `doc`.
>>> glide_json.strlen(client, "doc", "nested.a")
    5  # The length of the JSON value at path 'nested.a' in the key stored at `doc`.
>>> glide_json.strlen(client, "doc", "$")
    [None]  # Returns an array with None since the value at root path does in the JSON document stored at `doc` is not
            # a string.
>>> glide_json.strlen(client, "non_existing_key", ".")
    None  # `key` doesn't exist.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
def strlen(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> TJsonResponse[Optional[int]]:
    """
    Returns the length of the JSON string value stored at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document. Default to None.

    Returns:
        TJsonResponse[Optional[int]]:
            For JSONPath (`path` starts with `$`):
                Returns a list of integer replies for every possible path, indicating the length of the JSON string value,
                or None for JSON values matching the path that are not string.
            For legacy path (`path` doesn't start with `$`):
                Returns the length of the JSON value at `path` or None if `key` doesn't exist.
                If multiple paths match, the length of the first mached string is returned.
                If the JSON value at `path` is not a string of if `path` doesn't exist, an error is raised.
            If `key` doesn't exist, None is returned.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> import json
        >>> glide_json.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
            'OK'
        >>> glide_json.strlen(client, "doc", "$..a")
            [3, 5, None]  # The length of the string values at path '$..a' in the key stored at `doc`.
        >>> glide_json.strlen(client, "doc", "nested.a")
            5  # The length of the JSON value at path 'nested.a' in the key stored at `doc`.
        >>> glide_json.strlen(client, "doc", "$")
            [None]  # Returns an array with None since the value at root path does in the JSON document stored at `doc` is not
                    # a string.
        >>> glide_json.strlen(client, "non_existing_key", ".")
            None  # `key` doesn't exist.
    """

    return cast(
        TJsonResponse[Optional[int]],
        client.custom_command(
            ["JSON.STRLEN", key, path] if path else ["JSON.STRLEN", key]
        ),
    )

toggle(client, key, path)

Toggles a Boolean value stored at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path TEncodable

The path within the JSON document. Default to None.

required

Returns:

Type Description
TJsonResponse[bool]

TJsonResponse[bool]: For JSONPath (path starts with $): Returns a list of boolean replies for every possible path, with the toggled boolean value, or None for JSON values matching the path that are not boolean. If key doesn't exist, an error is raised. For legacy path (path doesn't start with $): Returns the value of the toggled boolean in path. If the JSON value at path is not a boolean of if path doesn't exist, an error is raised. If key doesn't exist, an error is raised.

TJsonResponse[bool]

For more information about the returned type, see TJsonResponse.

Examples:

>>> from glide import glide_json
>>> import json
>>> glide_json.set(
...     client,
...     "doc",
...     "$",
...     json.dumps({"bool": True, "nested": {"bool": False, "nested": {"bool": 10}}})
... )
    'OK'
>>> glide_json.toggle(client, "doc", "$.bool")
    [False, True, None]  # Indicates successful toggling of the Boolean values at path '$.bool' in the key stored at
                         # `doc`.
>>> glide_json.toggle(client, "doc", "bool")
    True  # Indicates successful toggling of the Boolean value at path 'bool' in the key stored at `doc`.
>>> json.loads(glide_json.get(client, "doc", "$"))
    [{"bool": True, "nested": {"bool": True, "nested": {"bool": 10}}}] # The updated JSON value in the key stored at
                                                                       # `doc`.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
def toggle(
    client: TGlideClient,
    key: TEncodable,
    path: TEncodable,
) -> TJsonResponse[bool]:
    """
    Toggles a Boolean value stored at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (TEncodable): The path within the JSON document. Default to None.

    Returns:
        TJsonResponse[bool]:
            For JSONPath (`path` starts with `$`):
                Returns a list of boolean replies for every possible path, with the toggled boolean value,
                or None for JSON values matching the path that are not boolean.
                If `key` doesn't exist, an error is raised.
            For legacy path (`path` doesn't start with `$`):
                Returns the value of the toggled boolean in `path`.
                If the JSON value at `path` is not a boolean of if `path` doesn't exist, an error is raised.
                If `key` doesn't exist, an error is raised.
        For more information about the returned type, see `TJsonResponse`.

    Examples:
        >>> from glide import glide_json
        >>> import json
        >>> glide_json.set(
        ...     client,
        ...     "doc",
        ...     "$",
        ...     json.dumps({"bool": True, "nested": {"bool": False, "nested": {"bool": 10}}})
        ... )
            'OK'
        >>> glide_json.toggle(client, "doc", "$.bool")
            [False, True, None]  # Indicates successful toggling of the Boolean values at path '$.bool' in the key stored at
                                 # `doc`.
        >>> glide_json.toggle(client, "doc", "bool")
            True  # Indicates successful toggling of the Boolean value at path 'bool' in the key stored at `doc`.
        >>> json.loads(glide_json.get(client, "doc", "$"))
            [{"bool": True, "nested": {"bool": True, "nested": {"bool": 10}}}] # The updated JSON value in the key stored at
                                                                               # `doc`.
    """

    return cast(
        TJsonResponse[bool],
        client.custom_command(["JSON.TOGGLE", key, path]),
    )

type(client, key, path=None)

Retrieves the type of the JSON value at the specified path within the JSON document stored at key.

Parameters:

Name Type Description Default
client TGlideClient

The client to execute the command.

required
key TEncodable

The key of the JSON document.

required
path Optional[TEncodable]

The path within the JSON document. Default to None.

None

Returns:

Type Description
Optional[TJsonUniversalResponse[bytes]]

Optional[TJsonUniversalResponse[bytes]]: For JSONPath ('path' starts with '$'): Returns a list of byte string replies for every possible path, indicating the type of the JSON value. If path doesn't exist, an empty array will be returned. For legacy path (path doesn't starts with $): Returns the type of the JSON value at path. If multiple paths match, the type of the first JSON value match is returned. If path doesn't exist, None will be returned. If key doesn't exist, None is returned.

Optional[TJsonUniversalResponse[bytes]]

For more information about the returned type, see TJsonUniversalResponse.

Examples:

>>> from glide import glide_json
>>> glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
    'OK'
>>> glide_json.type(client, "doc", "$.nested")
    [b'object']  # Indicates the type of the value at path '$.nested' in the key stored at `doc`.
>>> glide_json.type(client, "doc", "$.nested.a")
    [b'integer']  # Indicates the type of the value at path '$.nested.a' in the key stored at `doc`.
>>> glide_json.type(client, "doc", "$[*]")
    [b'integer',  b'object']  # Array of types in all top level elements.
Source code in doc-gen/valkey-glide/python/glide-sync/glide_sync/sync_commands/glide_json.py
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
def type(
    client: TGlideClient,
    key: TEncodable,
    path: Optional[TEncodable] = None,
) -> Optional[TJsonUniversalResponse[bytes]]:
    """
    Retrieves the type of the JSON value at the specified `path` within the JSON document stored at `key`.

    Args:
        client (TGlideClient): The client to execute the command.
        key (TEncodable): The key of the JSON document.
        path (Optional[TEncodable]): The path within the JSON document. Default to None.

    Returns:
        Optional[TJsonUniversalResponse[bytes]]:
            For JSONPath ('path' starts with '$'):
                Returns a list of byte string replies for every possible path, indicating the type of the JSON value.
                If `path` doesn't exist, an empty array will be returned.
            For legacy path (`path` doesn't starts with `$`):
                Returns the type of the JSON value at `path`.
                If multiple paths match, the type of the first JSON value match is returned.
                If `path` doesn't exist, None will be returned.
            If `key` doesn't exist, None is returned.
        For more information about the returned type, see `TJsonUniversalResponse`.

    Examples:
        >>> from glide import glide_json
        >>> glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
            'OK'
        >>> glide_json.type(client, "doc", "$.nested")
            [b'object']  # Indicates the type of the value at path '$.nested' in the key stored at `doc`.
        >>> glide_json.type(client, "doc", "$.nested.a")
            [b'integer']  # Indicates the type of the value at path '$.nested.a' in the key stored at `doc`.
        >>> glide_json.type(client, "doc", "$[*]")
            [b'integer',  b'object']  # Array of types in all top level elements.
    """
    args = ["JSON.TYPE", key]
    if path:
        args.append(path)

    return cast(Optional[TJsonUniversalResponse[bytes]], client.custom_command(args))