Create JSON - omit null values?


I’m using a module which accepts JSON input. I have created a data structure that’s mapped from a previous module. Now, I don’t always need to map all fields, and the unmapped fields are serialized as null, but the target module API won’t accept this; I need to omit those fields entirely. I understand I can create a new data structure without those fields, but I don’t really want to do that. Is there any option or filter I can use in order to omit null values?

Using the ignore keyword on all unused fields seems to do what I need. Still interested in a more automatic solution, however.

That’s what the ignore property is for actually.