Josh Glover
Josh Glover

Reputation: 26150

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.

Upvotes: 0

Views: 329

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51470

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.

Upvotes: 1

Related Questions