Custom JSON Object in Salesforce

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:

  1. writeStartObject    : This creates the start of the outermost object ‘{‘
  2. writeStringField    : This creates the key-value pair
  3. writeFieldName      : This creates key for an inner object
  4. writeStartArray     : This creates start of an inner object ‘[‘
  5. writeEndArray       : This creates end of an inner object ‘]’
  6. writeEndObject      : This creates the end of the outermost object ‘}’
  7. getAsString         : This returns a the JSON created as String type

Happy Coding 🙂

Advertisements

3 thoughts on “Custom JSON Object in Salesforce”

  1. 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

    Like

    1. 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 🙂

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s