This package provides support for the [ValkeyJSON](https://valkey.io/docs/stack/json/) module, which adds JSON as a native data type to Valkey. It extends the [Node Valkey client](https://github.com/firassziedan/node-valkey) to include functions for each
npm install valkey-jsonThis package provides support for the ValkeyJSON module, which adds JSON as a native data type to Valkey. It extends the Node Valkey client to include functions for each of the ValkeyJSON commands.
To use these extra commands, your Valkey server must have the ValkeyJSON module installed.
For a complete example, see managing-json.js in the Node Valkey examples folder.
The JSON.SET command stores a JSON value at a given JSON Path in a Valkey key.
Here, we'll store a JSON document in the root of the Valkey key "mydoc":
``javascript
import { createClient } from 'valkey';
...
await client.json.set('nodevalkey:jsondata', '$', {
name: 'Roberta McDonald',
pets: [
{
name: 'Rex',
species: 'dog',
age: 3,
isMammal: true
},
{
name: 'Goldie',
species: 'fish',
age: 2,
isMammal: false
}
]
});
`
For more information about ValkeyJSON's path syntax, check out the documentation.
With ValkeyJSON, we can retrieve all or part(s) of a JSON document using the JSON.GET command and one or more JSON Paths. Let's get the name and age of one of the pets:
`javascript`
const results = await client.json.get('nodevalkey:jsondata', {
path: [
'.pets[1].name',
'.pets[1].age'
]
});
results will contain the following:
`javascript`
{ '.pets[1].name': 'Goldie', '.pets[1].age': 2 }
ValkeyJSON includes commands that can atomically update values in a JSON document, in place in Valkey without having to first retrieve the entire document.
Using the JSON.NUMINCRBY command, we can update the age of one of the pets like this:
`javascript`
await client.json.numIncrBy('nodevalkey:jsondata', '.pets[1].age', 1);
And we can add a new object to the pets array with the JSON.ARRAPPEND command:
`javascript``
await client.json.arrAppend('nodevalkey:jsondata', '.pets', {
name: 'Robin',
species: 'bird',
age: 1,
isMammal: false
});