Module: Valkey::Commands::JsonCommands

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/json_commands.rb

Overview

This module contains commands related to RedisJSON / Valkey JSON.

Instance Method Summary collapse

Instance Method Details

#json_arrappend(key, path, *values) ⇒ Array<Integer>

Append values to JSON array at path.

Examples:

Append to array

valkey.json_arrappend("user:1", "$.tags", '"ruby"', '"valkey"')
  # => [3]

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • values (Array<String>)

    JSON values to append

Returns:

  • (Array<Integer>)

    array of new array lengths

See Also:



189
190
191
# File 'lib/valkey/commands/json_commands.rb', line 189

def json_arrappend(key, path, *values)
  send_command(RequestType::JSON_ARR_APPEND, [key, path] + values)
end

#json_arrindex(key, path, value, start = nil, stop = nil) ⇒ Array<Integer>

Get index of value in JSON array.

Examples:

Find index

valkey.json_arrindex("user:1", "$.tags", '"ruby"')
  # => [0]

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • value (String)

    the value to search for

  • start (Integer) (defaults to: nil)

    optional start index

  • stop (Integer) (defaults to: nil)

    optional stop index

Returns:

  • (Array<Integer>)

    array of indices (-1 if not found)

See Also:



207
208
209
210
211
212
# File 'lib/valkey/commands/json_commands.rb', line 207

def json_arrindex(key, path, value, start = nil, stop = nil)
  args = [key, path, value]
  args << start.to_s if start
  args << stop.to_s if stop
  send_command(RequestType::JSON_ARR_INDEX, args)
end

#json_arrinsert(key, path, index, *values) ⇒ Array<Integer>

Insert values into JSON array at index.

Examples:

Insert at index

valkey.json_arrinsert("user:1", "$.tags", 1, '"python"')
  # => [3]

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • index (Integer)

    the index to insert at

  • values (Array<String>)

    JSON values to insert

Returns:

  • (Array<Integer>)

    array of new array lengths

See Also:



227
228
229
# File 'lib/valkey/commands/json_commands.rb', line 227

def json_arrinsert(key, path, index, *values)
  send_command(RequestType::JSON_ARR_INSERT, [key, path, index.to_s] + values)
end

#json_arrlen(key, path = nil) ⇒ Array<Integer, nil>

Get length of JSON array at path.

Examples:

Get array length

valkey.json_arrlen("user:1", "$.tags")
  # => [2]

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Array<Integer, nil>)

    array of array lengths

See Also:



242
243
244
245
246
# File 'lib/valkey/commands/json_commands.rb', line 242

def json_arrlen(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_ARR_LEN, args)
end

#json_arrpop(key, path = nil, index = nil) ⇒ Array<String, nil>

Pop element from JSON array.

Examples:

Pop last element

valkey.json_arrpop("user:1", "$.tags")
  # => ["\"ruby\""]

Pop at index

valkey.json_arrpop("user:1", "$.tags", 0)
  # => ["\"python\""]

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

  • index (Integer) (defaults to: nil)

    optional index (default: -1)

Returns:

  • (Array<String, nil>)

    array of popped values

See Also:



263
264
265
266
267
268
# File 'lib/valkey/commands/json_commands.rb', line 263

def json_arrpop(key, path = nil, index = nil)
  args = [key]
  args << path if path
  args << index.to_s if index
  send_command(RequestType::JSON_ARR_POP, args)
end

#json_arrtrim(key, path, start, stop) ⇒ Array<Integer>

Trim JSON array to specified range.

Examples:

Trim array

valkey.json_arrtrim("user:1", "$.tags", 0, 1)
  # => [2]

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • start (Integer)

    start index

  • stop (Integer)

    stop index

Returns:

  • (Array<Integer>)

    array of new array lengths

See Also:



283
284
285
# File 'lib/valkey/commands/json_commands.rb', line 283

def json_arrtrim(key, path, start, stop)
  send_command(RequestType::JSON_ARR_TRIM, [key, path, start.to_s, stop.to_s])
end

#json_clear(key, path = nil) ⇒ Integer

Clear container values at path.

Examples:

Clear array

valkey.json_clear("user:1", "$.tags")
  # => 1

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Integer)

    number of paths cleared

See Also:



332
333
334
335
336
# File 'lib/valkey/commands/json_commands.rb', line 332

def json_clear(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_CLEAR, args)
end

#json_debug(subcommand, key, path = nil) ⇒ Object

Get debug information about JSON value.

Examples:

Get memory usage

valkey.json_debug("MEMORY", "user:1", "$")
  # => [120]

Parameters:

  • subcommand (String)

    the debug subcommand

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Object)

    depends on subcommand

See Also:



365
366
367
368
369
# File 'lib/valkey/commands/json_commands.rb', line 365

def json_debug(subcommand, key, path = nil)
  args = [subcommand, key]
  args << path if path
  send_command(RequestType::JSON_DEBUG, args)
end

#json_del(key, path = nil) ⇒ Integer

Delete JSON value at path.

Examples:

Delete entire document

valkey.json_del("user:1")
  # => 1

Delete specific path

valkey.json_del("user:1", "$.age")
  # => 1

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path (default: root)

Returns:

  • (Integer)

    number of paths deleted

See Also:



62
63
64
65
66
# File 'lib/valkey/commands/json_commands.rb', line 62

def json_del(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_DEL, args)
end

#json_forget(key, path = nil) ⇒ Integer

Alias for json_del (deprecated).

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Integer)

    number of paths deleted

See Also:



75
76
77
78
79
# File 'lib/valkey/commands/json_commands.rb', line 75

def json_forget(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_FORGET, args)
end

#json_get(key, *paths) ⇒ String?

Get JSON value at path.

Examples:

Get entire JSON document

valkey.json_get("user:1")
  # => "{\"name\":\"John\",\"age\":30}"

Get specific path

valkey.json_get("user:1", "$.name")
  # => "[\"John\"]"

Parameters:

  • key (String)

    the key

  • paths (Array<String>)

    optional paths to retrieve

Returns:

  • (String, nil)

    JSON string or nil if key doesn’t exist

See Also:



24
25
26
27
# File 'lib/valkey/commands/json_commands.rb', line 24

def json_get(key, *paths)
  args = [key] + paths
  send_command(RequestType::JSON_GET, args)
end

#json_mget(*keys_and_path) ⇒ Array<String, nil>

Get multiple JSON values.

Examples:

Get from multiple keys

valkey.json_mget("user:1", "user:2", "$.name")
  # => ["[\"John\"]", "[\"Jane\"]"]

Parameters:

  • keys_and_path (Array<String>)

    keys followed by path

Returns:

  • (Array<String, nil>)

    array of JSON strings

See Also:



91
92
93
# File 'lib/valkey/commands/json_commands.rb', line 91

def json_mget(*keys_and_path)
  send_command(RequestType::JSON_MGET, keys_and_path)
end

#json_numincrby(key, path, value) ⇒ String

Increment number at path.

Examples:

Increment age

valkey.json_numincrby("user:1", "$.age", 1)
  # => "[31]"

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • value (Numeric)

    the increment value

Returns:

  • (String)

    JSON array of new values

See Also:



124
125
126
# File 'lib/valkey/commands/json_commands.rb', line 124

def json_numincrby(key, path, value)
  send_command(RequestType::JSON_NUM_INCR_BY, [key, path, value.to_s])
end

#json_nummultby(key, path, value) ⇒ String

Multiply number at path.

Examples:

Multiply price

valkey.json_nummultby("product:1", "$.price", 1.1)
  # => "[110.0]"

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • value (Numeric)

    the multiplier

Returns:

  • (String)

    JSON array of new values

See Also:



140
141
142
# File 'lib/valkey/commands/json_commands.rb', line 140

def json_nummultby(key, path, value)
  send_command(RequestType::JSON_NUM_MULT_BY, [key, path, value.to_s])
end

#json_objkeys(key, path = nil) ⇒ Array<Array<String>>

Get keys of JSON object at path.

Examples:

Get object keys

valkey.json_objkeys("user:1", "$")
  # => [["name", "age"]]

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Array<Array<String>>)

    array of key arrays

See Also:



298
299
300
301
302
# File 'lib/valkey/commands/json_commands.rb', line 298

def json_objkeys(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_OBJ_KEYS, args)
end

#json_objlen(key, path = nil) ⇒ Array<Integer, nil>

Get number of keys in JSON object at path.

Examples:

Get object length

valkey.json_objlen("user:1", "$")
  # => [2]

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Array<Integer, nil>)

    array of key counts

See Also:



315
316
317
318
319
# File 'lib/valkey/commands/json_commands.rb', line 315

def json_objlen(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_OBJ_LEN, args)
end

#json_resp(key, path = nil) ⇒ Object

Get JSON value in RESP format.

Examples:

Get as RESP

valkey.json_resp("user:1", "$")
  # => [["name", "John"], ["age", 30]]

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Object)

    RESP representation

See Also:



382
383
384
385
386
# File 'lib/valkey/commands/json_commands.rb', line 382

def json_resp(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_RESP, args)
end

#json_set(key, path, value) ⇒ String

Set JSON value at path.

Examples:

Set entire document

valkey.json_set("user:1", "$", '{"name":"John","age":30}')
  # => "OK"

Set specific path

valkey.json_set("user:1", "$.age", "31")
  # => "OK"

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • value (String)

    the JSON value

Returns:

  • (String)

    “OK”

See Also:



44
45
46
# File 'lib/valkey/commands/json_commands.rb', line 44

def json_set(key, path, value)
  send_command(RequestType::JSON_SET, [key, path, value])
end

#json_strappend(key, path, value) ⇒ Array<Integer>

Append string to JSON string at path.

Examples:

Append to name

valkey.json_strappend("user:1", "$.name", '" Jr."')
  # => [8]

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

  • value (String)

    the string to append

Returns:

  • (Array<Integer>)

    array of new string lengths

See Also:



156
157
158
# File 'lib/valkey/commands/json_commands.rb', line 156

def json_strappend(key, path, value)
  send_command(RequestType::JSON_STR_APPEND, [key, path, value])
end

#json_strlen(key, path = nil) ⇒ Array<Integer, nil>

Get length of JSON string at path.

Examples:

Get string length

valkey.json_strlen("user:1", "$.name")
  # => [4]

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Array<Integer, nil>)

    array of string lengths

See Also:



171
172
173
174
175
# File 'lib/valkey/commands/json_commands.rb', line 171

def json_strlen(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_STR_LEN, args)
end

#json_toggle(key, path) ⇒ Array<Integer>

Toggle boolean value at path.

Examples:

Toggle boolean

valkey.json_toggle("user:1", "$.active")
  # => [1]

Parameters:

  • key (String)

    the key

  • path (String)

    the JSON path

Returns:

  • (Array<Integer>)

    array of new boolean values (0 or 1)

See Also:



349
350
351
# File 'lib/valkey/commands/json_commands.rb', line 349

def json_toggle(key, path)
  send_command(RequestType::JSON_TOGGLE, [key, path])
end

#json_type(key, path = nil) ⇒ Array<String>

Get the type of JSON value at path.

Examples:

Get type

valkey.json_type("user:1", "$.age")
  # => ["integer"]

Parameters:

  • key (String)

    the key

  • path (String) (defaults to: nil)

    optional path

Returns:

  • (Array<String>)

    array of type names

See Also:



106
107
108
109
110
# File 'lib/valkey/commands/json_commands.rb', line 106

def json_type(key, path = nil)
  args = [key]
  args << path if path
  send_command(RequestType::JSON_TYPE, args)
end