Bad JSON string with json_encode on empty table

Hello :slightly_smiling_face:

For log purpose i’m using a custom log_hooks to add some metadata then send a message to a RabbitMQ queue.
But recently some issues came up : services consuming the queue can’t parse the incoming JSON, a field (tls_peer_subject_name) defined as an array is written as an empty object ({} instead of an empty array [])

Indeed in my log_hooks when i manually add a field as an empty array data["tls_peer_subject_name"] = {} the data retrieved in the RabbitMQ queue looks like this { ..., "type": "Rejection", "tls_peer_subject_name": {}, ...}.

It seems that kumo.serde.json_encode and kumo.serde.json_encode_pretty are using materialize_to_lua_value which will try to obtain a native lua representation of the provided value unfortunately that’s where the problem happens when encoding empty table as correct Lua representation but as invalid JSON’s one :confused:

To hackfix this i added a check for size of this table in my log_hooks.

Do you have another workaround ? (Or an incoming feature/fix ? :eyes: )

Thanks by advance and happy new year ! :partying_face:

PS:
Using version 2025.10.06-5ec871ab :

kumo.serde.json_encode(toto))```
will give :
`{'empty_json':{},'empty_lua':{},'filled':[1,2,3]}`

bad JSON string with json_encode on empty table

It’s an ambiguous data representation. Lua tables are simultaneously an array and an object from javascript’s perspective. In the absence of something to provide a hint (such as an internal Rust type, or by sampling the non-empty contents of the table), an empty js array and an empty js object both expand to an empty lua table.

When you json encode an empty lua table, it has no way to know which one you mean in javascript, so the encoder just picks one.

I would recommend not setting an empty table, just omit the field in that case, and you’ll avoid the ambiguity.