0

Normally when I json_encode and set a value in Redis it gets stored as a string. How do I store a value as JSON

I've tried the following :

$redisCon = Redis::connection('default');

$data = $redisCon->executeRaw(['JSON.new', 'someKeyValue', '{"math":0}']);

but the response keep saying false and the data does not get saved.

I've got RedisJSON support on my redis instance, I'm running the redis/redis-stack-server:latest docker container to check this.

4
  • 3
    I think redis does not accept json/objects. Just convert your json object to string when saving and back to json when retrieving Commented Sep 19, 2023 at 5:34
  • @Mr.Kenneth, you should add this as an answer, as this is the correct response for OP's question. Commented Sep 19, 2023 at 6:15
  • Redis has support for JSON Objects with full-text indexing also redis.io/docs/data-types/json Commented Sep 19, 2023 at 6:35
  • Ah I solved it :) $data = $redisCon->executeRaw(['JSON.SET', 'someKeyValue', '{"math":0}']); Commented Sep 19, 2023 at 6:51

1 Answer 1

1

I manged to solve it, I was using the wrong command JSON.NEW, use JSON.SET instead and also put the path with $.

$data = $redisCon->executeRaw(['JSON.set', 'someKeyValue', '$', '{"math":0}']);
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.