Map with empty values are omitted by default

aurajavascriptlightning-aura-componentsmap

Below is the way I store the keys and values in my map.

Map<String, Object> mapNew = new Map<String, Object>();
Object obj = record.get(fieldName) != null ? record.get(fieldName) : null;    
mapNew.put(fieldMap.get(fieldName).getDescribe().getLabel(), obj); // contains field label and corresponding field value

Response in system.debug is

recordDetail={Account Name=Test, Active=true, Business Phone=null, [email protected], Title=Mr.}

But when I am printing the same map on the javascript side, response looks like this,

Account Name: "Test"
Active: true
Email: "[email protected]"
Title: "Mr."

I want to know why the key with an empty value entry gets omitted automatically and how to handle this issue in javascript side.

Best Answer

I don't know the specifics as to why key's with null values in maps are omitted when passing data to the client, but, a simple enough operation would be to convert your map to a string. Then, from the client, when receiving the server payload, simply use JSON.parse(variable) in order to convert back to an object.

Related Topic