5

For some strange reason when i try to use the JSON generator to generate some basic JSON using a specific format its not putting a comma between two objects..

here is a simplified example of what im doing

JSONGenerator gen = JSON.createGenerator(true);

gen.writeStartObject();

gen.writeFieldName('folder-1');
gen.writeStartArray();

gen.writeStartObject();
gen.writeStringField('filename', 'fake.jpg');
gen.writeEndObject();

gen.writeEndArray();
gen.writeEndObject();


gen.writeStartObject();

gen.writeFieldName('folder-2');
gen.writeStartArray();

gen.writeStartObject();
gen.writeStringField('filename', 'fake2.jpg');
gen.writeEndObject();

gen.writeEndArray();
gen.writeEndObject();

String pretty = gen.getAsString().replace('\n', '');

return pretty;

and its outputing ALMOST perfectly as

{  "folder-1" : [ {    "filename" : "fake.jpg"  } ]} {  "folder-2" : [ {    "filename" : "fake2.jpg"  } ]}

but you'll notice there isnt an comma between the two outer objects? i know im close but what im i doing wrong?

2

While not a direct answer to your question I wanted to note that it's often much easier to nest apex data structures and invoke JSON.serialize on that.

Your sample code could be rewritten (including the inclusion of array suggested by other answers) to:

List<Object> rawData = new List<Object>{ //a list of, well, anything really
    new Map<String,Object>{ //first JSON object
        'folder-1' => new List<Object>{
            new Map<String,Object>{
                'filename' => 'fake.jpg'
            }
        }
    },
    new Map<String,Object>{ //second JSON object
        'folder-2' => new List<Object>{
            new Map<String,Object>{
                'filename' => 'fake2.jpg'
            }
        }
    }
};
JSON.serialize(rawData);

This format is, at least to me and those I've discussed it with, much easier to visualize when reading the code, and can be constructed dynamically in really any type of structure rather than using the static definition demonstrated here to achieve the same flexibility as the JSONGenerator. The difference is that this is often much easier to work with, and leaves out the risk of unbalanced or invalid JSON being returned.

1

For anyone else that gets stuck with this generator due to order dependent JSON interpreters. The issue that OP is having is related to his use of writeEndObject.

gen.writeStartObject();
    gen.writeFieldName('getHostedPaymentPageRequest');
    gen.writeStartObject();
    gen.writeFieldName('merchantAuthentication');
    gen.writeStartObject();
    gen.writeStringField('name',struct.getHostedPaymentPageRequest.merchantAuthentication.name);
    gen.writeStringField('transactionKey',struct.getHostedPaymentPageRequest.merchantAuthentication.transactionKey);
    gen.writeEndObject();
    gen.writeFieldName('transactionRequest');
    gen.writeStartObject();
    gen.writeStringField('transactionType',struct.getHostedPaymentPageRequest.transactionRequest.transactionType);

Notice in the above, when I am getting ready to move to the next object that I want to be separated by a Comma, I don't need to use writeEndObject in that case. When I start the next field, the closing brace is then added with a comma and then an opening brace starts.

In the above, when he does writeEndObject and writeStartObject it is doing those literally and not assuming that you are making two JSON objects right next to each other. You don't need the start and end to try and force the comma. It may not be a correct JSON format to not use arrays, but I have no say in the systems I connect to. I think this is the real answer to the question he is asking and an issue that I sat and just struggled with for over 2 hours today...

{"getHostedPaymentPageRequest" : {
     "merchantAuthentication" : {
       "name" : "abc123",
       "transactionKey" : "09876543"
     },
     "transactionRequest" : {
       "transactionType" : "authCaptureTransaction"
      ......
0

Came across this same issue, and you do need to put all of the code inside an array but like this:

gen.writeStartArray();

// YOUR CODE HERE 

gen.writeEndArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.