Reserved Keywords in Salesforce Apex JSON String

Reserved Keywords in Salesforce Apex JSON String

Map data type allows us to handle or manage reserved keywords in Apex String.

Sample Code:

Map < String, String > mapFinal = new Map < String, String > {
    'example' => 'Test1'
};
Map < String, String > mapGlobal = new Map < String, String > {
    'Sample' => 'Test2'
};
Map < String, String > mapBody = new Map < String, String >();
mapBody.put(
    'final',
    JSON.serialize(
    	mapFinal
    )
);
mapBody.put(
    'global',
    JSON.serialize(
    	mapGlobal
    )
);
mapBody.put(
    'abstract',
    'Test3'
);
mapBody.put(
    'false',
    'Test4'
);
mapBody.put(
    'for',
    'Test5'
);
mapBody.put(
    'asc',
    'Test6'
);
mapBody.put(
    'if',
    'Test7'
);
System.debug(
    JSON.serialize(
    	mapBody
    ).replaceAll(
    	'\\\\',
        ''
    ).replace(
        '":"{"',
        '":{"'
    ).replace(
        '"}"',
        '"}'
    )
);

Leave a Reply