2

I am tring to make this code workig with python.

This is the code used:

r = redis.StrictRedis(host='192.168.22.108', port=6379, db=0, decode_responses=True) #Docker servers.

your_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
parsed = json.loads(your_json)
print(json.dumps(parsed, indent=4, sort_keys=True))

r.append('sabado', (parsed))

print ("Recuperamos de REDIS: ")
recuperado = r.get('sabado')
print ("tipo de variable de recuperado: "+str(type(recuperado)))
parsed2 = json.loads(recuperado)
print(json.dumps(parsed2, indent=4, sort_keys=True))


I am obteining this error:

Recuperamos de REDIS: 
tipo de variable de recuperado: <type 'unicode'>
Traceback (most recent call last):
File "pruebas_web.py", line 17, in <module>
    parsed2 = json.loads(recuperado)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Basicaly what I am tring to make is to store a Json in redis a get it back.

2 Answers 2

2

You're appending the desizerialized form of the JSON to the key. Change as follows:

# r.append('sabado', (parsed))
r.set('sabado', your_json)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

import redis

r = redis.Redis(host='0.0.0.0', port='6379')

jane = {'name': "Jane",'Age': 33,'Location': "Chawton"}

r.set('d2', json.dumps(jane))

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.