function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MugwumpusMugwumpus 

unexpected token: JSONGenerator

Getting this error (Error: Compile Error: unexpected token: JSONGenerator at line 11 column 11) when saving a new class, any thoughts? 
Ankit AroraAnkit Arora
Code snippet would be helpful here, are you doing like this?

// Pass true to the constructor for pretty print formatting.
JSONGenerator gen = JSON.createGenerator(true);
// Write data to the JSON string.
gen.writeStartObject();
gen.writeNumberField('abc', 1.21);
gen.writeStringField('def', 'xyz');
gen.writeFieldName('ghi');
gen.writeStartObject();
        
gen.writeObjectField('aaa', intlist);
        
gen.writeEndObject();
        
gen.writeFieldName('Object A');
        
gen.writeObject(x);
        
gen.writeEndObject();

// Get the JSON string.
String pretty = gen.getAsString();


MugwumpusMugwumpus
I'm working through the integration workbook.  Here's the code snippet:
JSONGenerator gen = JSON.createGenerator(true);
        // open the JSON generator
        gen.writeStartArray();
        // interate through the list of invoices passed in to the call
        // writing each invoice Id to the array
        for (Id invoiceId : invoiceIds) {
            gen.writeStartObject();
            gen.writeStringField('id', invoiceId);
            gen.writeEndObject();           
        }
        // close the JSON generator
        gen.writeEndArray();
        // create a string from the JSON generator
        String jsonOrders = gen.getAsString();
        // debugging call, which you can check in console logs
        System.debug('jsonOrders: ' + jsonOrders);
David "w00t!" LiuDavid "w00t!" Liu
The only reference to JSONGenerator I see in your code is the first line, but supposedly the error is on line 11!

The problem could be from code that you have before this line, if any (ie missing semi-colon in the line before)?  Is there any code that you're not posting?
Robin AlmoesRobin Almoes
I have had the same error while going through the workbook and found what caused it. Just pasting it here for people having the same problem.
Above the code in the workbook is a button to copy the code to your clipboard. If you use that to copy the code from the workbook and paste it into your Apes class, it will NOT paste correctly.
The solution is to copy the code by selecting it in its entirety. Then it will paste correctly.