2

I have been trying to figure out how I can set key and value to a specific column. By column I mean something like:

"fruit": {
    "american" {
        "key": "value",
        "key2": "value2"
    },
    "europe" {
        "key": "value"
        "key2": "value2"
    }
},

"books": {
    "american_author" {
        "key": "value"
        "key2": "value2"
    },
    "asia_author" {
        "key": "value"
        "key2": "value2"
    }
},

"paint": {
    "africa" {
        "key": "value"
        "key2": "value2"
    },
    "south_america" {
        "key": "value"
        "key2": "value2"
    }
}

What im trying to achieve here is that I would like to be able to add a new "column" which is fruit, book and paint and inside those values I would like to add another "column" and inside each column I want to add keys and values. As you can see in the snippet above.

For now I have done something like this:

import serialized_redis

r = serialized_redis.JSONSerializedRedis(host='localhost', port=6379, db=0)
r.set('fruit', 'american', {'key': 'value' })

but what returns:

    raise DataError("Invalid input of type: '%s'. Convert to a "
redis.exceptions.DataError: Invalid input of type: 'dict'. Convert to a bytes, string, int or float first.

My question is, am I able to do it using Redis and if so, how can I be able to add the keys and values to a specific "column" as given at the top of the thread?

2 Answers 2

4

You can encode the nested JSON part as a string as use Redis Hash

For example, 'fruit', 'books', 'paint' , etc can be a redis hash, 'american', 'europe', etc can be the key of the hash and 'key', 'key2' can be stored as value of the key as JSON string. Like the following:

redisClient = redis.Redis(host='localhost', port=6379, db=0)

# your application logic to form the json
american_json = {"key": "value", "key2": "value2"}
europe_json = {"key": "value", "key2": "value2"}

# hash name: fruit; # hash-key1: american; #value of hash-key1: JSON as string
redisClient.hset("fruit", "american", json.dumps(american_json))    
redisClient.hset("fruit", "europe", json.dumps(europe_json))

If at this point you check redis:

127.0.0.1:6379> hgetall fruit
1) "american"
2) "{\"key\": \"value\", \"key2\": \"value2\"}"
3) "europe"
4) "{\"key\": \"value\", \"key2\": \"value2\"}"

Further code logic to add new fields:

# say you have to add another key-value in the key "american" for the hash "fruit"
#first we get the key stored in redis

fruit_american_json_string = redisClient.hget("fruit", "american")

#then we convert the retrieved string to JSON
JSON_object = json.loads(fruit_american_json_string)

#add your new key
JSON_object["key3"] = "value3"

#write the new JSON as string in Redis
redisClient.hset("fruit", "american", json.dumps(JSON_object))

Final output in redis:

127.0.0.1:6379> hgetall fruit
1) "american"
2) "{\"key\": \"value\", \"key2\": \"value2\", \"key3\": \"value3\"}"
3) "europe"
4) "{\"key\": \"value\", \"key2\": \"value2\"}"
Sign up to request clarification or add additional context in comments.

1 Comment

Very clever way! I did not know that it was actually that easy! I just tested it and it seemed to work exactly the way I wished to do it. Thank you so much! I will try again some more stuff but its exactly what I was looking for. Would it be ok if I comment again if I have some question? I will mark this as the answer for now :)
1

You can accomplish this by using the Hash data type with HSET

https://redis-py.readthedocs.io/en/stable/#redis.Redis.hset

Comments

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.