Search code examples
jsonredisredis-cliredisjson

json object append in redis-cli


i am using redis-cli for one of my project and i need to append the data into existing json in redis, i have tried json.arrappend but it is not working. i need to append in sDetail array and in jsonDetails array. any suggestions how to append in json in redis-cli

'{"xyz": [{"subType": 1,"sDetail": [{"eCs": "3","jsonDetails": "{\"ce\" :[{\"cRId\":272, \"cV\":10000, \"type\":1, \"tId\":0, \"uTid\":\"T00005\", \"sNumber\":\"53320\", \"sDetailId\":1101}]}"}]}]}'

Solution

  • jsonDetails is a string and not an array

    127.0.0.1:6379> JSON.SET test $ '{"xyz": [{"subType": 1,"sDetail": [{"eCs": "3","jsonDetails": "{\"ce\" :[{\"cRId\":272, \"cV\":10000, \"type\":1, \"tId\":0, \"uTid\":\"T00005\", \"sNumber\":\"53320\", \"sDetailId\":1101}]}"}]}]}'
    OK
    127.0.0.1:6379> JSON.TYPE test $.xyz[0].sDetail[0].jsonDetails
    1) "string"
    

    If you set it like this

    JSON.SET test $ '{"xyz": [{"subType": 1,"sDetail": [{"eCs": "3","jsonDetails": {"ce" :[{"cRId":272, "cV":10000, "type":1, "tId":0, "uTid":"T00005", "sNumber":"53320", "sDetailId":1101}]}}]}]}'
    

    It would be an array

    127.0.0.1:6379> JSON.TYPE test $.xyz[0].sDetail[0].jsonDetails.ce
    1) "array"
    

    And now you can append to it using JSON.ARRAPPEND, for example:

    127.0.0.1:6379> JSON.ARRAPPEND test $.xyz[0].sDetail[0].jsonDetails.ce '{"new": "data"}'
    1) (integer) 2
    

    To append to sDetail you can try something like

    JSON.ARRAPPEND test $.xyz[0].sDetail '{"eCs": "42", "jsonDetails": {"ce": [{"cRId":999, "cV": 888}]}}'