You need to sign in to do that
Don't have an account?

Best method to generate JSON string for HTTP callouts
Hi,
What is the best practice to generate JSON string for HTTP callouts? The options I know are:
1) Using String variables and methods
2) Use JSON.serialize function after defining a class
3) Use JSONGenerator
Using the above 3 methods, if we need to change the request, we have to change the code. Is there any other good solution so that the code need not be chaged even if the JSON request needs to be changed?
What is the best practice in generating JSON request?
Thanks.
What is the best practice to generate JSON string for HTTP callouts? The options I know are:
1) Using String variables and methods
2) Use JSON.serialize function after defining a class
3) Use JSONGenerator
Using the above 3 methods, if we need to change the request, we have to change the code. Is there any other good solution so that the code need not be chaged even if the JSON request needs to be changed?
What is the best practice in generating JSON request?
Thanks.
If you store the information needed to generate the JSOn in custom object/s or Custom metadata type (depending on the complexity), it will help you to not update the code any time the JSON payload is changed.
For example ,
Lets say your JSON payload is as follows :
{
"FirstName":"James",
"LastName":"Bond",
}
So the custom object / CMT where will look something like below ,
1 Record
Key : FirstName,
ApiName : FirstName
Object : Contact
2 Record
Key : LastName,
ApiName : LastName
Object : Contact
And while forming the JSON you would pick the the keys from this object/ CMT to form the JSON payload.
The Value can be queried by using the object and its Field Names mentined in the custom object.
You can even store the query somewhere so that you know what needs to be queried.
So in short whatever you need to form the JSON payload willl be stored as Static. The values in the Payload will be dynamic.
In future you can just change the static values if needed so that you can form the JSON payload ass you want.
Hope this helps.
All Answers
If you store the information needed to generate the JSOn in custom object/s or Custom metadata type (depending on the complexity), it will help you to not update the code any time the JSON payload is changed.
For example ,
Lets say your JSON payload is as follows :
{
"FirstName":"James",
"LastName":"Bond",
}
So the custom object / CMT where will look something like below ,
1 Record
Key : FirstName,
ApiName : FirstName
Object : Contact
2 Record
Key : LastName,
ApiName : LastName
Object : Contact
And while forming the JSON you would pick the the keys from this object/ CMT to form the JSON payload.
The Value can be queried by using the object and its Field Names mentined in the custom object.
You can even store the query somewhere so that you know what needs to be queried.
So in short whatever you need to form the JSON payload willl be stored as Static. The values in the Payload will be dynamic.
In future you can just change the static values if needed so that you can form the JSON payload ass you want.
Hope this helps.
Below Sample Code for JSON Generator can fulfill your requirements. Hope this will work for you.
public class JsonstringgeneratefromAccountdata {
public void jsongenerate(){
JSONGenerator gen = JSON.createGenerator(true);
gen.writeString('Account Data');
gen.writeStartArray();
for(Account actobj:[select id,name,type,Industry from Account limit 2]){
gen.writeStartObject();
gen.writeStringField('Account Name',actobj.Name);
gen.writeStringField('Account Type',actobj.Type);
gen.writeStringField('Industry',actobj.Industry);
gen.writeEndObject();
}
gen.writeEndArray();
System.debug('Json Account Structure:'+gen.getAsString());
}
}
Please mark this as best answer if this solves your problem.
Thank you,
Ajay Dubedi