3

I have been trying to figure out how to get multiple values into a key for example:

{
    "fruit": {
        "tomato": {
            "Color": "red",
            "Price": "100"
        },
        "banana": {
            "Color": "yellow",
            "Price": "150"
        }
    }
}

For now my currently code is:

r = serialized_redis.MsgpackSerializedRedis(host='localhost', port=6379, db=0)
r.set("fruit", {"tomato": {"Color": "red", "Price": "100"}})
r.set("fruit", {"banana": {"Color": "yellow", "Price": "150"}})

The problem is that everytime that I do a r.set it seems that it replaces so meaning that when I run this code it will just be set of:

{
    "fruit": {
        "banana": {
            "Color": "yellow",
            "Price": "150"
        }
    }
}

so even if I do a r.set of "tomato" it will be replaced by "banana" since its the latest one that is being SET.

My question is, how can I add it to the same Key but with different values so that everything is in the same key but it has different "furits"?

{
    "fruit": {
        "tomato": {
            "Color": "red",
            "Price": "100"
        },
        "banana": {
            "Color": "yellow",
            "Price": "150"
        }
    }
}
4
  • This might help (even though it does not use py-redis): stackoverflow.com/questions/20271372/redis-list-of-nested-keys Commented Aug 27, 2020 at 19:16
  • You can also always just store for example your dictionary as a JSON string instead. Commented Aug 27, 2020 at 19:18
  • @Timbolt yes but if I just want to be able to add one value etc as I show in example th en I might not be able to do it if I use set? - I did have hard time to understand the link you showed :( Commented Aug 27, 2020 at 19:23
  • 1
    Correct, you would need to read in the JSON to a dict, modify the dict and insert the entire JSON again. Probably not a great solution here, but still good to know since it can come in very handy to use this method in other situations. Commented Aug 28, 2020 at 9:00

1 Answer 1

2

you can use hash for that

hmset fruit tomato your_json_serialized_here
hmset fruit orange ...

you can do hmset with multiple fruit too like this hmset fruit apple 1 banana 2 orange 3

accessing data from hash with hmget is like this hmget fruit orange banana apple

Sign up to request clarification or add additional context in comments.

3 Comments

Im not sure how that would work? I tried to do r.hmset("fruit", {"tomato": {"Color": "red", "Price": "100"}}) but that returned WRONGTYPE Operation against a key holding the wrong kind of value What am I missing?
you have to serialize the object or stringify it hmset fruit tomato <the-json-stringify-of-object"
Just a heads up that HMSET is deprecated as of version 4.0.0. HSET can be used instead: redis.io/commands/hset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.