Skip to main content

JSON Module

The JSON module provides a set of commands to work with JSON documents and is available starting at v1.20.0 in @upstash/redis.

Supported Commands

All examples below assume, you have created an @upstash/redis instance named. redis.

ARRAPPEND

Append the json values into the array at path after the last element in it.

const value = ...
redis.json.arrappend(key, "$.path.to.array", value);

ARRINDEX

Search for the first occurrence of a JSON value in an array.

const value = ...
redis.json.arrindex(key, "$.path.to.array", value);

ARRINSERT

Insert the json values into the array at path before the index (shifts to the right).

const value = ...
const index = 2
redis.json.arrinsert(key, "$.path.to.array", index, value);

ARRLEN

Report the length of the JSON array at path in key.

redis.json.arrlen(key, "$.path.to.array");

ARRPOP

Remove and return an element from the index in the array.

By default the last element from an array is popped.

redis.json.arrpop(key, "$.path.to.array");

Or specify the index to pop from.

const index = 2;
redis.json.arrpop(key, "$.path.to.array", index);

ARRTRIM

Trim an array so that it contains only the specified inclusive range of elements.

redis.json.arrtrim(key, "$.path.to.array");

Or specify the start and end index.

redis.json.arrtrim(key, "$.path.to.array", 2, 4);

CLEAR

Clear container values (arrays/objects) and set numeric values to 0.

redis.json.clear(key, "$.path");

DEL

Delete a value.

redis.json.del(key, "$.path");

FORGET

See DEL.

redis.json.forget(key, "$.path");

GET

Return the value at path in JSON serialized form.

Get a single value from a JSON document.

redis.json.get(key, "$.path");

Get multiple values from a JSON document.

redis.json.get(key, "$.path1", "$.path2");

MGET

Return the values at path from multiple key arguments.

redis.json.mget([key1, key2], "$.path");

NUMINCRBY

Increment the number value stored at path by number.

redis.json.numincrby(key, "$.path", 2);

NUMMULTBY

Multiply the number value stored at path by number.

redis.json.nummultby(key, "$.path", 2);

OBJKEYS

Return the keys in the object that's referenced by path.

redis.json.objkeys(key, "$.path");

OBJLEN

Report the number of keys in the JSON object at path in key.

redis.json.objlen(key, "$.path");

RESP

Return the JSON in key in Redis serialization protocol specification form.

redis.json.resp(key, "$.path");

SET

Set the JSON value at path in key.

const value = ...
redis.json.set(key, "$.path", value);

Or you can set the value only if the path does not exist.

const value = ...
redis.json.set(key, "$.path", value, {nx:true});

Or you can set the value only if the path exists.

const value = ...
redis.json.set(key, "$.path", value, {xx:true});

STRAPPEND

Append the json-string values to the string at path.

redis.json.strappend(key, "$.path", "foo");

STRLEN

Report the length of the JSON String at path in key.

redis.json.strlen(key, "$.path");

TOGGLE

Toggle a boolean value stored at path.

redis.json.toggle(key, "$.path");

TYPE

Report the type of JSON value at path.

redis.json.type(key, "$.path");