Skip to content
Salesforce Factory

Salesforce Factory

A Complete Blog on Salesforce and Beyond

  • Home
  • Products
  • Authors
  • Contact

Tag: JSON Structure for Salesforce SObjects

Posted on August 30, 2019August 30, 2019

JSON Structure for Salesforce SObjects

  • tweet 
  • share 
  • share 

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]

  • tweet 
  • share 
  • share 

Enter Keywords

Categories

  • Admin Posts (6)
  • Developer Posts (20)
  • Discussions (1)
  • Lightning Communities (1)

Archives

  • February 2024 (2)
  • April 2023 (1)
  • March 2021 (1)
  • January 2021 (1)
  • September 2020 (1)
  • August 2020 (1)
  • June 2020 (3)
  • September 2019 (1)
  • August 2019 (1)
  • July 2019 (1)
  • March 2019 (4)
  • February 2019 (1)
  • November 2018 (1)
  • October 2018 (9)

Recent Posts

  • Apex Programming – Safe Navigation Operator February 15, 2024
  • Apex Programming – Null Coalescing Operator February 15, 2024
  • Create Salesforce Custom Object using Apex Class April 11, 2023
  • Deleting Apex Classes / Apex Triggers from Production using Workbench March 2, 2021
  • Salesforce Spring ’21 Highlights / KeyPoints / Non-Lightning January 12, 2021

About This Site

Saleforce Factory

A Complete Blog on Salesforce and Beyond

Site Statistics
  • Today's visitors: 3
  • Total visitors : 93,544
Proudly powered by WordPress