jsonclojurecheshire

Can Cheshire omit keys without values?


I'm using Cheshire to generate some JSON for data structures like this:

(require '[cheshire.core :refer [generate-string])
(generate-string {:id 123, :foo "something", :bar nil})

Which produces JSON like this:

{"id": 123, "foo": "something", "bar": null}

What I'd like is for the JSON to omit the keys without values; e.g.

{"id": 123, "foo": "something"}

Can Cheshire do this? I can certainly pre-filter the map before calling generate-string, but since Cheshire has to traverse my data structure anyway, I thought it would be more performant to instruct Cheshire to do the filtering.


Solution

  • No, null is a valid JSON value, so you should filter nil values yourself.

    See this question for more info.

    You may propose this feature to Cheshire team.