Skip to main content
Retrieves the JSON value stored at the specified key and optionally formats the output.

Usage

const result = await redis.json.get<UserData>("user:123");

Arguments

key
string
required
The key containing the JSON document
opts
object
Optional formatting options for the returned JSON string
path
string | string[]
JSONPath expression(s) to specify which parts of the JSON to retrieve. If not specified, returns the entire JSON document.

Response

result
TData | null
The JSON value at the specified path, or null if the key doesn’t exist. The return type is generic and can be specified using TypeScript.

Examples

Get entire JSON document

interface User {
  name: string;
  email: string;
  age: number;
}

const user = await redis.json.get<User>("user:123");
console.log(user);
// { name: "Alice", email: "[email protected]", age: 30 }

Get specific path

const name = await redis.json.get<string>("user:123", "$.name");
console.log(name); // ["Alice"]

Get with formatting options

const formatted = await redis.json.get(
  "user:123",
  {
    indent: "  ",
    newline: "\n",
    space: " "
  },
  "$"
);

Get multiple paths

const data = await redis.json.get("user:123", "$.name", "$.email");

JSONPath Syntax

Upstash Redis supports JSONPath expressions for querying JSON documents:
  • $ - Root element
  • $.field - Access a field
  • $.field[0] - Access array element
  • $..field - Recursive descent
  • $.field[*] - All array elements

See Also

Build docs developers (and LLMs) love