@tstirrat in the example code wrapped with the check for the `embeddedRecord` it appears that `json[key]` is never set. Perhaps I should have asked what would be best, `json[key] j= null`, `json[key] = {}` or not setting `json[key]` at all? 

Since the current code returns an error `Cannot call method 'serialize' of null` for an optional (embedded) record `json[key] = null` does seem the best choice. At least better than not setting anything at all.