Create a Custom JSON in apex code
Apex is the coding language for Salesforce cloud computing.
Salesforce provides a Class ‘JSONGenerator’ using which one can serialize a collection, wrapper class into JSON or can deserialize in reverse.
Using this class a Custom JSON can also be written, below is the code snippet for the same:
JSONGenerator jsonObj = JSON.createGenerator(true); jsonObj.writeStartObject(); jsonObj.writeStringField('Container', 'Outer'); jsonObj.writeFieldName('Wrapper'); jsonObj.writeStartArray(); jsonObj.writeStartObject(); jsonObj.writeStringField('innerWrapper1', 'Value1'); jsonObj.writeStringField('innerWrapper2', 'Value2'); jsonObj.writeStringField('innerWrapper3', 'Value3'); jsonObj.writeEndObject(); jsonObj.writeEndArray(); jsonObj.writeEndObject(); String finalJSON = jsonObj.getAsString();
The output of the above snippet when debugged will give:
'{"Container" : "Outer", "Wrapper" : [ { "innerWrapper1" : "Value1", "innerWrapper2" : "Value2", "innerWrapper3" : "Value3" } ] }'
Decoding each method of the JSONGenerator class:
- writeStartObject : This creates the start of the outermost object ‘{‘
- writeStringField : This creates the key-value pair
- writeFieldName : This creates key for an inner object
- writeStartArray : This creates start of an inner object ‘[‘
- writeEndArray : This creates end of an inner object ‘]’
- writeEndObject : This creates the end of the outermost object ‘}’
- getAsString : This returns a the JSON created as String type
Happy Coding 🙂
Very informative, this gets me about 90% where I am going. I have a single issue perhaps you can help me with.
my end json string should be this:
“devices”: [
{
“deviceIds”: [
{
“id”: “value”,
“kind”: “value”
}
]
}
]
}
When I use the sample you provided it looks like this:
JSONGenerator jsonObj = JSON.createGenerator(true);
jsonObj.writeStartObject();
jsonObj.writeStringField(‘devices’, ”);
jsonObj.writeFieldName(‘deviceIds’);
jsonObj.writeStartArray();
jsonObj.writeStartObject();
jsonObj.writeStringField(‘id’, ‘value’);
jsonObj.writeStringField(‘kind’, ‘value’);
jsonObj.writeEndObject();
jsonObj.writeEndArray();
jsonObj.writeEndObject();
String finalJSON = jsonObj.getAsString();
Which returns this:
{
“devices”: “”,
“deviceIds”: [
{
“id”: “values”,
“kind”: “values”
}
]
}
I can leave off the the first jsonObj.writeStringField(‘devices’, ”); but what I need to be able to do is have something like (2) two jsonObj.writeFieldName(‘deviceIds’); in row, which I can not seem to do.
Thanks and please advise.
Jerry
LikeLike
Hi Jerry,
Glad the content is helpful.
I tried creating the expected string for you, below snippet will get you what you need:
JSONGenerator jsonObj = JSON.createGenerator(true);
jsonObj.writeStartObject();//{
jsonObj.writeFieldName(‘devices’);
jsonObj.writeStartArray();//[
jsonObj.writeStartObject();//{
jsonObj.writeFieldName(‘deviceIds’);
jsonObj.writeStartArray();//[
jsonObj.writeStartObject();//{
jsonObj.writeStringField(‘id’, ‘value’);
jsonObj.writeStringField(‘kind’, ‘value’);
jsonObj.writeEndObject();//}
jsonObj.writeEndArray();//]
jsonObj.writeEndObject();//}
jsonObj.writeEndArray();//]
jsonObj.writeEndObject();//}
String finalJSON = jsonObj.getAsString();
Happy Coding 🙂
LikeLike
Works great, thanks!
LikeLike