+ Start a Discussion
anil Kumaranil Kumar 

Prepare Dynamic JSON Body

Hi All,
I would like to prepare Dynamic JSON Body on the below mentioned format.
{
                "MyData": [{
                                "id": "SFDC-Contact1",
                                "name": "SFDC-Contact",
                                "description": "Contact Details",
                                "Popularity": "Public",
                                "dataTypes": [{
                                                "name": "Id",
                                                "description": "ID of Contact",
                                }, {
                                                "name": "Name",
                                                "description": "Complete name of Contact"
                                }],
                                "FCLR": null,
                                "DMS": null
                }]
}

I have used json string generator for a POST request body from salesforce to a third party.

list<FieldDefinition> sct = [SELECT DeveloperName,DataType,Label,LastModifiedDate FROM FieldDefinition WHERE EntityDefinition.DeveloperName = 'Contact'];

system.debug('=============='+sct.size() );

JSONGenerator gen = JSON.createGenerator(true);

  gen.writeStartObject();
  gen.writeFieldName('MyData');
  
  gen.writeStartObject();
  
  gen.writeStringField('id', 'SFDC-Contact1');
  gen.writeStringField('name', 'SFDC-Contact');
  gen.writeEndObject();
  gen.writeEndObject();
  gen.writeStartObject();

  gen.writeFieldName('dataTypes');
  gen.writeStartArray();
 
     for (FieldDefinition s: sct ){
        gen.writeStartObject();
        gen.writeStringField('name', s.Label);
  
        gen.writeStringField('description', 'ID of Contact');
        gen.writeEndObject();
      }         

gen.writeEndArray();

gen.writeEndObject();

String pretty = gen.getAsString();
system.debug('=============='+pretty );

Below is JSON body i am getting. Please advise.
{
  " MyData " : {
    "id" : "SFDC-Contact1",
    "name" : "SFDC-Contact"
  }
} {
  "dataTypes" : [ {
    "name" : "Contact ID",
    "description" : "ID of Contact"
  }, {
    "name" : "Deleted",
    "description" : "ID of Contact"
  } ]
}
pconpcon
I would not use JSONGenerator for this.  You can do this with standard Apex functionality
public class MyDataType {
    String name;
    String description;

    public MyDataType(String name, String description) {
        this.name = name;
        this.description = description;
    }
}

public class MyContact {
    public String id;
    public String name;
    public String description;
    public String popularity;
    public List<MyDataType> dataTypes;
    public String FCLR;
    public String DMS;

    public MyContact(Contact c) {
        this.id = c.Id;
        this.name = c.Name;
        this.description = c.Description;
        this.popularity = c.Popularity__c;

        this.dataTypes = new List<MyDataType> {
            new MyDataType("Id", "ID of Contact"),
            new MyDataType("Name", "description": "Complete name of Contact")
        };
    }
}

public class Wrapper {
    List<MyContact> MyData;

    public Wrapper() {}

    public void addContact(Contact c) {
        this.MyData.add(new MyContact(c));
    }
}

Wrapper w = new Wrapper();
for (Contact c : [
    select Name,
        Description,
        Popularity__c
]) {
    w.addContact(c);
}

String output = JSON.serialize(w);
anil Kumaranil Kumar
Hi pcon,

Thank you for the your responce, i have used JSONGenerator since if the user has created new field i  need send that field to third party org dynamically without code modification. 

Thanks,
Anil Kumar