Skip to content
Advertisement

How to create a column and set key and value to redis using Python

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

JavaScript

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:

JavaScript

but what returns:

JavaScript

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?

Advertisement

Answer

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:

JavaScript

If at this point you check redis:

JavaScript

Further code logic to add new fields:

JavaScript

Final output in redis:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement