You need to sign in to do that
Don't have an account?
anil 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"
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"
} ]
}
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