1

I looking at the code on how to generate a JSON structure like below:

1.      {
        "element1": "123123",
        "element2": "test",
        "element3": [{
            "element3.1": " ",
            "element3.2": " ",
            "element3.3": " ",
            "element3.4": " ",
            "element3.5": " "
        }],
        "element5": "01",
        "element6": "123333",
}

On researching I am able to find out the way to generate JSON for element 1 and 2 but I want some help to know how to generate JSON structure for element 3 onwards.

So far I tried this :

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('element1', 123123);
gen.writeStringField('element2', test);
// How to generate element3 structure?
gen.writeEndObject();
  1. How do I go about if the structure starts with an array?

    {

    "elementAA": [{
    "element1": "123123",
     "element2": "test",
    "element3": [{
                "element3.1": " ",
                "element3.2": " ",
    }],
            "element5": "01",
            "element6": "123333"
    }]
    
    }
    
1
  • you have to use writeStartArray() method
    – Ratan Paul
    Commented Apr 10, 2017 at 5:35

1 Answer 1

5

Don't bother using JSONGenerator. It's particularly error prone and slower than just serializing a map. Here's an example of serializing a map:

JSON.serialize(
  new Map<String, Object> {
    'element1' => '123123',
    'element2' => 'test',
    'element3' => new Object[] {
      new Map<String, Object> {
        'element3.1' => ' ',
        'element3.2' => ' ',
        'element3.3' => ' ',
        'element3.4' => ' ',
        'element3.5' => ' '
      }
    },
    'element5' => '01',
    'element6' => '123333'
  }
);

It's usually pretty easy to initialize once you get use to it, and you don't have to remember to start and arrays, end objects and arrays, worry about checking if a field is a number, string, null, etc.

If you find code on the Internet in Java that uses JSONGenerator, you can copy-paste it in to Apex Code, but I'd recommend you avoid using this if you're writing code from the ground up.

6
  • Great. Thank you. This will get me started. I have question about how will I begin the code if the JSON structure starts with an array ( pls see my part 2 in the question for JSON structure)
    – SfdcBat
    Commented Apr 10, 2017 at 5:48
  • @SfdcBat It's the same as the example: new Map<String, Object> { 'elementAA' => new Object[] { new Map<String, Object> { 'element1' => '123123' ... You can combine List and Map recursively as you need to.
    – sfdcfox
    Commented Apr 10, 2017 at 5:53
  • Can this be assigned to a variable type? I am trying to use system.debug to print and view the strucutre
    – SfdcBat
    Commented Apr 10, 2017 at 5:59
  • @SfdcBat Yes, you can assign it to a Map<String, Object>, debug it into a checkpoint, etc.
    – sfdcfox
    Commented Apr 10, 2017 at 6:03
  • Thanks. I am looking to use the JSOn string in req.setBody(JsonStructure); method. If I assign Map<String,Object> jsonString = new Map<String, Object> { 'elementAA' => new Object[] { new Map<String, Object> { 'element1' => '123123' ... , How can I use the jsonString variable in req.setBody(jsonString ); ?
    – SfdcBat
    Commented Apr 10, 2017 at 6:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .