how can i save a complex json as string in redis and retrieve it as unescaped legit json object

870 views Asked by At

the final aim is to save and retrieve a json as string in a redis instance, i am getting this json from an external api and would like to persist it in my redis instance.

i am able to save the document using redis module using node. now when i try to retrieve the value it gives me an escaped json string. Following is my code to save data to redis against a key:

app.get('/util/util1/:key', function(req, res) {
  res.type('application/json');
  var name = req.params.key;
 res.header("Access-Control-Allow-Origin", "*");

  var responseserv;
  client.get(name, function(err, reply) {

    console.log("Parameter: " + JSON.stringify(JSON.parse(reply)));
    //var unescapedstring = JSON.stringify(JSON.parse(reply),null,4);
    //console.log(unescapedstring);
    res.send(deepUnescapeJSON(JSON.parse(reply)));   
    });
});

The Code to fetch the value of a key is as follows:

app.post('/util/util1/addtoredis/:key', function(req, res) {
    res.type('application/json');

    var redvalue = JSON.stringify(req.body);
    console.log("Body : " + req.url+" body : "+redvalue);
    var redkey = req.params.key;
    console.log("key & value    :: " + redkey + " , " + (redvalue));
    client.set(redkey, redvalue);
    //console.log("IP Details : "+myIP());
    //console.log("Got request: " + JSON.stringify(redvalue));
    res.send(redvalue);     
});

I find that if i store json in redis it is stored as a escaped string while when i fetch it it is retrieved as the same escaped string.

following is the sample json:

{
    "name": "Sample newspaper",
    "description": "Sample newspaper",
    "expiredOn": "22-nov-2010",
    "isDynamic": false,
    "isEnabled": false,
    "startedOn": "22-oct-2010",
    "updatedOn": "",
    "updatedBy": "admin",
    "newspaperRules": [
        {
            "type": "4",
            "buyCount": "0",
            "buyAmount": "200",
            "placeHolderText": "Helix times",
            "count": 1,
            "id": "",
            "newspaperFreeItems": [
                {
                    "id": "",
                    "groupId": 1,
                    "itemId": "23456",
                    "type": "1",
                    "isActive": 1
                }
            ]
        }
    ]
}
0

There are 0 answers