user1187968
user1187968

Reputation: 8006

Redis - HMSET a json object

What's the correct way to store a JSON object as the value for a pair using HMSET in red? Should I escape the JSON object like this below?

HMSET myhash field1 "{\"k1\":1}" 

Upvotes: 2

Views: 2375

Answers (2)

Suyog Kale
Suyog Kale

Reputation: 373

You'll need to serialize your object to a string. Consequently, you will also need to be able to deserialize that string back to an instance of your class. One option for serializing/deserializing would be JSON.

Upvotes: 1

Deepak Goyal
Deepak Goyal

Reputation: 62

If you are using redis-cli, both the following commands return the same result:

> SET key1 '{"name":"Fred","age":25}'
> GET key1 
"{\"name\":\"Fred\",\"age\":25}"

> SET key2 "{\"name\":\"Fred\",\"age\":25}"
> GET key2
"{\"name\":\"Fred\",\"age\":25}"

Upvotes: 2

Related Questions