3

Say I have this json code

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

How could I store it to a redisdb via redis-py?

My code is the following (I believe it's wrong):

import json
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=1)
with open('json_test.json', encoding='utf-8') as data_file:
    test_data = json.loads(data_file.read())
r.hmset('test_json', test_data)
2
  • There is some reason why you are using hmset? Your goal is store full document under a single key? Commented Oct 11, 2015 at 13:33
  • No, I want to make an optimal storage of this .json file. My general goal is to store .json files in redis (and larger files than the one I gave) with the best possible structure (via redis-py). Commented Oct 11, 2015 at 13:37

1 Answer 1

4

Considering your code, and simplicity of requirement: Store JSON file content on redis, your can use simple SET for it.

To improve data parsing, it is not necessary to use .read() method over file pointer, consequently, you use json.load(fp) method version.

import json
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=1)
with open('json_test.json') as data_file:
    test_data = json.load(data_file)
r.set('test_json', test_data)
Sign up to request clarification or add additional context in comments.

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.