Set Json data into Redis

4,601 views
Skip to first unread message

ezeq

unread,
Mar 21, 2011, 5:18:15 PM3/21/11
to Redis DB
Hi to all,
i´m trying to save a json string of properties into a string field,
but when i´m looking the saved json, this
returns with quotes and other totally escaped....can us tell me how
must i do to save a clean json in a redis field
and return the same data without escape slashes?
Thanks!

Jonathan Leibiusky

unread,
Mar 21, 2011, 5:24:32 PM3/21/11
to redi...@googlegroups.com, ezeq
which client are you using to connect to redis?


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.


Scott Smith

unread,
Mar 21, 2011, 5:24:02 PM3/21/11
to redi...@googlegroups.com, ezeq
You could base64 encode it before saving to redis.

Jonathan Leibiusky

unread,
Mar 21, 2011, 5:34:57 PM3/21/11
to redi...@googlegroups.com, Scott Smith, ezeq
redis is not escaping any character, it is binary safe, so it should store and return exactly what you are sending, no need for base64. this is probably a client issue. that's why I am asking which client are you using so someone from the list can help :)

Scott Smith

unread,
Mar 21, 2011, 5:54:55 PM3/21/11
to Jonathan Leibiusky, redi...@googlegroups.com, ezeq
Yeah, for sure. I wasn't trying to blame redis :)

ezeq

unread,
Mar 22, 2011, 9:35:48 AM3/22/11
to Redis DB
Thanks for the answers, i´m using predis, but i don´t use base64
encode, only save a json string..



On 21 mar, 18:54, Scott Smith <sc...@ohlol.net> wrote:
> Yeah, for sure. I wasn't trying to blame redis :)
>
> On Mon, Mar 21, 2011 at 2:34 PM, Jonathan Leibiusky <ionat...@gmail.com>wrote:
>
> > redis is not escaping any character, it is binary safe, so it should store
> > and return exactly what you are sending, no need for base64. this is
> > probably a client issue. that's why I am asking which client are you using
> > so someone from the list can help :)
>
> > On Mon, Mar 21, 2011 at 6:24 PM, Scott Smith <sc...@ohlol.net> wrote:
>
> >> You could base64 encode it before saving to redis.
>

Jonathan Leibiusky

unread,
Mar 22, 2011, 10:17:17 AM3/22/11
to redi...@googlegroups.com
Maybe predis is escaping your string?

Daniele Alessandri

unread,
Mar 22, 2011, 10:18:38 AM3/22/11
to redi...@googlegroups.com
On Tue, Mar 22, 2011 at 14:35, ezeq <eclip...@gmail.com> wrote:

> Thanks for the answers, i´m using predis, but i don´t use base64
> encode, only save a json string..

Predis doesn't manipulate in any way the string values sent to (or
returned by) Redis. That said, can you please share a stripped-down
snippet of code to be able to reproduce your issue?

Thanks,
Daniele

--
Daniele Alessandri
http://clorophilla.net/
http://twitter.com/JoL1hAHN

ezeq

unread,
Mar 22, 2011, 10:41:26 AM3/22/11
to Redis DB
Here is the code

include '../Mobi/Mtld/DA/Api.php';
include 'cJSON.inc';

define("REDIS_HOST","localhost");
define("REDIS_PORT",5000);

$cJSON = new Services_JSON();
$queryResponse = array();

$ua = "Alcatel-BG3-color/1.0 UP.Browser/5.0.3.3.11";

echo $ua.'<br />';
echo "UA::".md5($ua).'<br />';

$tree = Mobi_Mtld_DA_Api::getTreeFromFile("json/datlas.json");
$properties = Mobi_Mtld_DA_Api::getProperties($tree, $ua); #
Array of properties
$queryResponse["UA::".md5($ua)] = $cJSON->encode($properties);

//echo $cJSON->encode($properties);

if(isset($properties['vendor']))
{

require_once 'predis/lib/Predis.php';
$single_server = array('host' => REDIS_HOST, 'port' => REDIS_PORT);
$r = new Predis_Client($single_server);

$r->set("UA::".md5($ua),$queryResponse["UA::".md5($ua)]);//$cJSON-
>encode($properties));

print $r->get("UA::".md5($ua));
}

# in this line the response is escaped
print_r($cJSON->encode($queryResponse));

I was thinking that in the previous line was the problem, because when
i do $r->get the response is correct, but, in redis-cli when i´m doing
the get call to this element, this info comes escaped too.






On 22 mar, 11:18, Daniele Alessandri <suppaki...@gmail.com> wrote:

Pieter Noordhuis

unread,
Mar 22, 2011, 10:48:18 AM3/22/11
to redi...@googlegroups.com, ezeq
Bulk replies in redis-cli are always escaped when they are printed to
a terminal. This is done to make sure no weird stuff happens when the
bulk might contain non-ascii characters. When you redirect the output
of redis-cli, it will not escape bulk replies.

Cheers,
Pieter

ezeq

unread,
Mar 22, 2011, 10:46:21 AM3/22/11
to Redis DB
I was trying to save a json string from redis-cli, but can´t do it,
you know why is giving an 'invalid argument' error

ezeq

unread,
Mar 22, 2011, 10:55:27 AM3/22/11
to Redis DB
Ahhh ok...so the problem is in the last JSON encode line, because when
i´m printing the response without encoding this returns fine
But i need to send this JSON encoded response...what can i do?

On 22 mar, 11:48, Pieter Noordhuis <pcnoordh...@gmail.com> wrote:
> Bulk replies in redis-cli are always escaped when they are printed to
> a terminal. This is done to make sure no weird stuff happens when the
> bulk might contain non-ascii characters. When you redirect the output
> of redis-cli, it will not escape bulk replies.
>
> Cheers,
> Pieter
>

Daniele Alessandri

unread,
Mar 22, 2011, 11:07:21 AM3/22/11
to redi...@googlegroups.com
On Tue, Mar 22, 2011 at 15:41, ezeq <eclip...@gmail.com> wrote:
[...cut...]

>        print $r->get("UA::".md5($ua));

Why don't you try to decode the JSON string returned of this GET to
see if you correctly get back the properties array? If you get an
invalid format error from the JSON decoder or escaped characters
inside the values (when they are not present in the values of the
original array, that is) than there's indeed something wrong with
Predis, but I'm quite sure this isn't the case:

print_r($cJSON->decode($r->get("UA::".md5($ua))));

PS: I don't know the cJSON library but I'm supposing here it provides
a decode method :-)

> I was thinking that in the previous line was the problem, because when
> i do $r->get the response is correct, but, in redis-cli when i´m doing
> the get call to this element, this info comes escaped too.

See Pieter's reply.

ezeq

unread,
Mar 22, 2011, 11:26:19 AM3/22/11
to Redis DB
Yes, the properties array is right...so, maybe i was confused when i
saw the data
escaped from the console too.
Thanks to everybody to understanding my poor english, and help me to
solve this, thanks again!



On 22 mar, 12:07, Daniele Alessandri <suppaki...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages