The requirement was to get the JSON Structure of an Account object for a third-party tool to Map the fields and Load the object fields.
The Structure needed was only to have the Object name and all the Field Names from the object like Below:
I simplified the way to extract the actual JSON Structure for an Object and get all the fields. Below I have given the code for getting the JSON structure for both API names of the fields and Label names.
Execute below in Execute Anonymous Window for JSON structure with API names of the fields
map<string, map<string,string> > fieldList = new map<string, map<string,string> >();
map<string,SObjectField> fList = schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();
MAP<String,String> fieldandtype = new MAP<String,String>();
for(string str: fList.keySet()){
fieldandtype.put(str,'');
}
fieldList.put('Account',fieldandtype);
system.debug('-->'+Json.serialize(fieldList));
Execute below in Execute Anonymous Window for JSON structure with Label names of the fields
map<string, map<string,string> > fieldList = new map<string, map<string,string> >();
map<string,SObjectField> fList = schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();
MAP<String,String> fieldandtype = new MAP<String,String>();
for(string str: fList.keySet()){
fieldandtype.put(fList.get(str).getDescribe().getLabel(),'');
}
fieldList.put('Account',fieldandtype);
system.debug('-->'+Json.serialize(fieldList));
After executing, Open Raw Log and Capture the Unformatted structure of the object and use the Online JSON formatter to get the formatted version like below.
This will help us to save time in extracting the JSON Structure of SObject.
–Ranjith T [08/30/2019]