Working With JSON Data
The Valkey JSON module provides an efficient way to store and manipulate structured data. With GLIDE, Valkey modules are supported as built-in APIs, allowing you to interact with JSON module commands through dedicated client methods.
Requirements
Section titled “Requirements”Before using a module, including the supported modules, it will need to be loaded into Valkey. See Modules Introduction on how to load modules into your Valkey service.
For more on version requirements, see our introduction on Modules API.
Examples
Section titled “Examples”import glide.api.GlideClient;import glide.api.models.configuration.GlideClientConfiguration;import glide.api.models.configuration.NodeAddress;import glide.api.commands.servermodules.Json;
// Both standalone and cluster mode are supportedGlideClientConfiguration config = GlideClientConfiguration.builder() .address(NodeAddress.builder().host("localhost").port(6379).build()) .build();
try (GlideClient client = GlideClient.createClient(config).get()) { String key = "testKey"; String path = "$"; String jsonValue = "{\"a\": 1.0, \"b\": 2}";
CompletableFuture<String> setResponseFuture = Json.set(client, key, path, jsonValue); String setResponse = setResponseFuture.get(); // "OK" assert setResponse.equals("OK");
CompletableFuture<String> getResponseFuture = Json.get(client, key).get(); String getResponse = getResponseFuture.get(); // jsonValue assert getResponse.equals("{\"a\": 1.0, \"b\": 2}");}import {GlideJson, GlideClient} from "@valkey/valkey-glide";
// Both standalone and cluster mode are supportedconst client = await GlideClient.createClient({ addresses: [{ host: "localhost", port: 6379 }],});
const value = {a: 1.0, b:2};const jsonStr = JSON.stringify(value);
// Sets the value at `doc` as a JSON object.const jsonSetResponse = await GlideJson.set(client,"doc", "$", jsonStr);console.log(jsonSetResponse); // 'OK' - Indicates successful setting of the value at path '$' in the key stored at `doc`.
// Gets the value at path '$' in the JSON document stored at `doc`.const jsonGetResponse = await GlideJson.get(client, "doc", {path: "$"});console.log(JSON.stringify(jsonGetResponse)); // [{"a": 1.0, "b": 2}] # JSON object retrieved from the key `doc`from glide import GlideClient, GlideClientConfiguration, NodeAddress, glide_jsonimport json
# Both standalone and cluster mode are supportedconfig = GlideClientConfiguration([NodeAddress("localhost", 6379)])client = await GlideClient.create(config)value = {'a': 1.0, 'b': 2}json_str = json.dumps(value) # Convert Python dictionary to JSON string using json.dumps()
# Sets the value at `doc` as a JSON object.set_response = await glide_json.set(client, "doc", "$", json_str)print(set_response) # "OK" - Indicates successful setting of the value at path '$' in the key stored at `doc`.
# Gets the value at path '$' in the JSON document stored at `doc`.get_response = await glide_json.get(client, "doc", "$")print(get_response) # b"[{\"a\":1.0,\"b\":2}]"json.loads(str(get_response)) # [{"a": 1.0, "b" :2}] - JSON object retrieved from the key `doc` using json.loads()import ( "context" glide "github.com/valkey-io/valkey-glide/go/v2" "github.com/valkey-io/valkey-glide/go/v2/config" "github.com/valkey-io/valkey-glide/go/v2/servermodules/glidejson")
// Both standalone and cluster mode are supportedcfg := config.NewClientConfiguration(). WithAddress(&config.NodeAddress{Host: "localhost", Port: 6379})client, err := glide.NewClient(cfg)
ctx := context.Background()key := "testKey"path := "$"jsonValue := `{"a": 1.0, "b": 2}`
// JSON.SETsetResponse, err := glidejson.JsonSet(client, ctx, key, path, jsonValue)// setResponse == "OK"
// JSON.GETgetResponse, err := glidejson.JsonGet(client, ctx, key)// getResponse contains {"a":1.0,"b":2}$client = new ValkeyGlide();$client->connect(addresses: [['host' => 'localhost', 'port' => 6379]]);
$key = "testKey";$path = "$";$jsonValue = '{"a": 1.0, "b": 2}';
// JSON.SET$client->jsonSet($key, $path, $jsonValue); // true
// JSON.GET$result = $client->jsonGet($key); // '[{"a":1.0,"b":2}]'$decoded = json_decode($result, true);using Valkey.Glide;using Valkey.Glide.ServerModules;
// Both standalone and cluster mode are supportedvar config = new ConnectionConfiguration.StandaloneClientConfigurationBuilder() .WithAddress("localhost", 6379) .Build();await using var client = await GlideClient.CreateClient(config);
var key = "testKey";var path = "$";var jsonValue = """{"a": 1.0, "b": 2}""";
// JSON.SETawait GlideJson.SetAsync(client, key, path, jsonValue);
// JSON.GETvar response = await GlideJson.GetAsync(client, key);Console.WriteLine($"Output: {response}"); // Output: {"a":1.0,"b":2}What’s Next
Section titled “What’s Next”For more details on the JSON module, see the Valkey JSON topic.