Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/upstash/redis-js/llms.txt

Use this file to discover all available pages before exploring further.

Sets the JSON value at the specified path in the key. Creates the key if it doesn’t exist.

Usage

await redis.json.set("user:123", "$", { name: "Alice", age: 30 });

Arguments

key
string
required
The key to store the JSON document
path
string
required
JSONPath expression specifying where to set the value in the JSON document. Use $ to set the entire document.
value
TData
required
The JSON value to set. Can be a number, string, boolean, object, or array.
opts
object
Optional conditional set options

Response

result
'OK' | null
Returns "OK" if the value was set successfully, or null if the condition (NX/XX) was not met.

Examples

Set entire JSON document

interface User {
  name: string;
  email: string;
  age: number;
  tags: string[];
}

const user: User = {
  name: "Alice",
  email: "alice@example.com",
  age: 30,
  tags: ["developer", "admin"]
};

await redis.json.set("user:123", "$", user);
// "OK"

Set specific field

await redis.json.set("user:123", "$.age", 31);
// "OK"

Set nested value

await redis.json.set("user:123", "$.address.city", "New York");

Set array element

await redis.json.set("user:123", "$.tags[0]", "senior-developer");

Conditional set - only if path doesn’t exist (NX)

const result = await redis.json.set(
  "user:123",
  "$.premium",
  true,
  { nx: true }
);

if (result === "OK") {
  console.log("Premium flag added");
} else {
  console.log("Premium flag already exists");
}

Conditional set - only if path exists (XX)

const result = await redis.json.set(
  "user:123",
  "$.age",
  32,
  { xx: true }
);

if (result === "OK") {
  console.log("Age updated");
} else {
  console.log("Age field doesn't exist");
}

JSONPath Syntax

  • $ - Root element (sets entire document)
  • $.field - Set a specific field
  • $.field[0] - Set array element at index
  • $.nested.field - Set nested field

See Also