Lua script converts empty array to object

Lua script converts empty array as an object. How to avoid conversion.


local json_str = '{\"items\":[],\"properties\":{}}'
return cjson.encode(cjson.decode(json_str))


redis-cli --eval test.lua


items are an array [] but the output is an object {}


  • The main difference between JSON object definition and lua table, that lua table has no type array.

    Empty JSON array [] or object {} is converted to lua table {}, but empty lua table {} can be converted to array [] or object {}.

    To my knowledge, cjson for redis has no solution for this problem at the moment, possible solution is mentioned in Redis Lua Differetiating empty array and object. (I can't argue if it works)