Can Cheshire omit keys without values?

312 Views Asked by At

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.

1

There are 1 best solutions below

0
Leonid Beschastny On BEST ANSWER

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.