Hi, I’m using Reitit with Muuntaja and Malli to build a REST API. I want to automatically convert incoming JSON body keys from camelCase to kebab-case in most cases. However, in some endpoints the JSON body contains keys that are UUIDs or user-defined identifiers (e.g. “Q1”, “123e4567-e89b-12d3-a456-426614174000"), and in those cases I need to preserve the original keys exactly as they are. What’s the best way to achieve this? I’ve considered: • using Malli schema with string? keys to avoid coercion, • doing reverse transformation manually in the handler, …but I’m not satisfied with either approach. Is there a recommended / idiomatic way to handle this? Or is it better to redesign the JSON format, e.g. move identifiers into values rather than keys? Thanks!
Using uuids as JSON keys can pose problems in a lot of environments. It can be done, but is not usually done as a lot of serialization / deserialization mechanisms are not well suited to have dynamic keys
Thanks for your advice. I found one solution for dynamic string keys: disabling key mapping in Muuntaja and using Malli’s key transformer, which can operate based on the Malli schema structure. But in general, avoiding dynamic keys makes sense, because there are other places where I use key mapping (e.g., loading JSON data from Postgres), which could lead to problems.
I think using :string (or perhaps :uuid) for those keys sounds right. In the past, I've often used keywords for static keys and strings for dynamic keys.
I've often ended up refactoring JSON APIs from something like
{"foo": 3, "bar": 4}
to
[{"name": "foo", "value": 3}, {"name": "bar", "value": 4}]
It's a bit less convenient when calling the API by hand, but can pay off in other ways.