cjsonredishiredisredisjson

How to get just the value from JSON.GET using RedisJSON, without the surrounding brackets and double quotes?


Using Redis v6 and RedisJSON v2.2.0.

Sample json:

[
        {
                "msg": "hello",
                "sql": "blah"
        }
]

I can successfully get the msg value by calling:

redisReply *reply = redisCommand(context, "JSON.GET sample-rj $.msg");

It returns:

["hello"]

However I would like the redis reply to not contain the surrounding brackets and double quotes.

Is that possible to turn off on the Redis/RedisJSON side or do I have to do post processing on the string value myself?

Thanks


Solution

  • I ended up doing this:

    redisReply *reply = redisCommand(context, "JSON.GET sample-rj $.msg");
    
        if(reply != NULL) {
    
            char *trimmedStr = reply->str + 2; //remove prefix ["
            trimmedStr[strlen(trimmedStr)-2] = '\0'; //remove suffix "]
    
            ap_rprintf(r, "<h2>redis key val is: %s!</h2>", trimmedStr);
    
            freeReplyObject(reply);
        }