You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/howtos/redisjson/using-python/index-usingpython.mdx
+61-80Lines changed: 61 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,144 +3,125 @@ id: index-usingpython
3
3
title: How to store JSON documents in Redis with Python
4
4
sidebar_label: RedisJSON and Python
5
5
slug: /howtos/redisjson/using-python
6
-
authors: [ajeet]
7
6
---
8
7
8
+
[RedisJSON](https://oss.redis.com/redisjson/) is a source-available Redis module that lets you store, manipulate, and query JSON documents in Redis. The standard Redis Python client (v4.0 or greater) supports all of the features of RedisJSON, and in this tutorial, we'll see how to get started with them.
9
9
10
-
RedisJSON is a Redis module that lets you store JSON documents in Redis. It is a JSON data type for Redis that lets you to fetch or modify a specific element in the document tree, without retrieving (or internally even parsing) the document. Its Python client even lets you store python dicts and convert them to JSON automatically.
10
+
## Getting started with RedisJSON
11
11
12
-
### RedisJSON Python Client
13
-
14
-
15
-
The 'rejson-py' is a package that allows storing, updating and querying objects as JSON documents in a Redis database that is extended with the RedisJSON module. The package extends redis-py's interface with RedisJSON's API, and performs on-the-fly serialization/deserialization of objects to/from JSON.
16
-
17
-
Follow the steps below to get started with RedisJSON with Python:
18
-
19
-
20
-
### Step 1. Run RedisJSON Docker container
12
+
To run the examples below, you'll need to ensure that you have an instance of Redis that includes RedisJSON. If you're developing locally, you can use Docker for this:
21
13
22
14
```bash
23
15
docker run -p 6379:6379 --name redis-redisjson redislabs/rejson:latest
24
16
```
25
17
18
+
## Verify that the RedisJSON module is loaded
26
19
27
-
### Step 2. Verify if RedisJSON module is loaded
20
+
Connect to Redis using `redis-cli`, and run the `info modules` command:
### Step 7. Fetching the specific fields within JSON document
79
+
In the code above, we first connect to Redis and store a reference to the connection in the `client` variable.
98
80
99
-
It's possible to fetch specific field within JSON element. Let us modify the above code and add "address" field as shown below:
81
+
Next, we create a Python dictionary to represent a person object.
100
82
101
-
```python
102
-
from rejson import Client, Path
83
+
And finally, we store the object in Redis using the `json().set()` method. The first argument, `person:1` is the name of the key that will reference the JSON. The second argument is a JSON path. We use `Path.rootPath()`, as this is a new object. Finally, we pass in the Python dictionary, which will be serialized to JSON.
103
84
85
+
To retrieve the JSON object, we run `json().get()`, passing in the key. The result is a Python dictionary representing the JSON object stored in Redis.
0 commit comments