function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hermann OuréHermann Ouré 

Print key in json format

Hello,
I have created a batch with a method that returns a json in a specific format (key-value pair)
 
private String prettyPrintLayoutFields(Map<String,String> layoutFieldMap){
        String result;
        result = layoutFieldMap.toString(); 
        System.debug('String result ' +result);
        return result;
    }
The value parsed into the field looks like this:
{Performance_Level__c=Performance Level, CreatedById=Created By, CreatedDate=Created By, LastModifiedById=Last Modified By, LastModifiedDate=Last Modified By, Name=Sub-Step Name, Level_of_details__c=Level of details, OwnerId=Owner, RecordTypeId=Record Type, ...}
How can I update this method to only print the key separated by a comma.
It looks like that I should use something like that:
string.join(map.keyset(),',');
But not quite on how to write it in my method
Could someone help?
Thanks

 
Best Answer chosen by Hermann Ouré
Maharajan CMaharajan C
Hi Hermann ,

Please try the below code :
 
private String prettyPrintLayoutFields(Map<String,String> layoutFieldMap){
	String result = '';
	set<string> strSet = layoutFieldMap.keySet();
	List<String> StrList = new List<String>();
	StrList.addall(strSet);
	result = String.join(StrList , ',');
	
	System.debug('String result ' +result);
	return result;
}

Thanks,
Maharajan.C