Welcome! Please see the About page for a little more info on how this works.

+1 vote
in data.json by

When running (json/write-str {:a 1 "a" 2}) it outputs ;; => "{\"a\":1,\"a\":2}". Should there be a validation to assert no duplicate keys exist? Or is it on the user to: 1. Not to mix keyword keys and string keys, and 2. Validate if there are no duplicate keys?

1 Answer

+1 vote
selected by
Best answer

All json keys are strings, so the default key-fn stringifies all key values. In general, most uses of the json write functions will be passing a map with either all keyword or all string keys - in those cases, it's not possible for any duplicate key to occur.

While it would be possible to transform the keys and/or track written keys, either of these approaches are expensive to perform given that they are usually not needed. Thus, you should ensure the stringified keys of your objects are unique prior to calling write, if there is some chance of duplication.