+ Start a Discussion
David Lee 8David Lee 8 

JSONGenerator and JSONParser do not work

I am trying to generate and parse JSON, but JSONGenerator and JSONParser do not seem to work. Execution stops without any errors.

for example, run the following code in the console:
 
List<Contact> contacts = [SELECT Id, Name FROM Contact LIMIT 10]; 
System.debug(1);
JSONGenerator generator = JSON.createGenerator(true);
System.debug(2);
generator.writeStartObject();
generator.writeNumberField('count', contacts.size())
generator.writeEndObject();
String jsonString = generator.getAsString();
System.debug(jsonString);
All I get is USER_DEBUG [14]|DEBUG|1. There are no errors in the output. Same happens when I try to use JSONParser.
 
Magesh Mani YadavMagesh Mani Yadav
HI David,
Try this. Should work
List<Contact> contacts = [SELECT Id, Name FROM Contact LIMIT 10]; 
System.debug(1);
JSONGenerator generator = JSON.createGenerator(true);
System.debug(2);
generator.writeStartObject();
generator.writeNumberField('count', contacts.size());
generator.writeEndObject();
String jsonString = generator.getAsString();
System.debug('jsonString: '+jsonString);


 
David Lee 8David Lee 8
Thanks, but I'm getting the same thing: USER_DEBUG [14]|DEBUG|1 And then execution just stops.
Magesh Mani YadavMagesh Mani Yadav
For me its woking. see the log
37.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
Execute Anonymous: List<Contact> contacts = [SELECT Id, Name FROM Contact LIMIT 10]; 
Execute Anonymous: System.debug(1);
Execute Anonymous: JSONGenerator generator = JSON.createGenerator(true);
Execute Anonymous: System.debug(2);
Execute Anonymous: generator.writeStartObject();
Execute Anonymous: generator.writeNumberField('count', contacts.size());
Execute Anonymous: generator.writeEndObject();
Execute Anonymous: String jsonString = generator.getAsString();
Execute Anonymous: System.debug('jsonString: '+jsonString);

15:55:57.3 (3576817)|EXECUTION_STARTED
15:55:57.3 (3581091)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
15:55:57.3 (3871905)|VARIABLE_SCOPE_BEGIN|[1]|contacts|List<Contact>|true|false
15:55:57.3 (3939959)|VARIABLE_SCOPE_BEGIN|[3]|generator|System.JSONGenerator|true|false
15:55:57.3 (3963990)|VARIABLE_SCOPE_BEGIN|[8]|jsonString|String|false|false
15:55:57.3 (4110423)|HEAP_ALLOCATE|[72]|Bytes:3
15:55:57.3 (4163553)|HEAP_ALLOCATE|[77]|Bytes:152
15:55:57.3 (4182240)|HEAP_ALLOCATE|[342]|Bytes:408
15:55:57.3 (4200987)|HEAP_ALLOCATE|[355]|Bytes:408
15:55:57.3 (4218622)|HEAP_ALLOCATE|[467]|Bytes:48
15:55:57.3 (4261996)|HEAP_ALLOCATE|[139]|Bytes:6
15:55:57.3 (4291592)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:17
15:55:57.3 (4463793)|STATEMENT_EXECUTE|[1]
15:55:57.3 (4467154)|STATEMENT_EXECUTE|[1]
15:55:57.3 (4471876)|HEAP_ALLOCATE|[1]|Bytes:37
15:55:57.3 (4485854)|HEAP_ALLOCATE|[1]|Bytes:4
15:55:57.3 (4533439)|HEAP_ALLOCATE|[50]|Bytes:5
15:55:57.3 (4560813)|HEAP_ALLOCATE|[56]|Bytes:5
15:55:57.3 (4571132)|HEAP_ALLOCATE|[64]|Bytes:7
15:55:57.3 (4808081)|SOQL_EXECUTE_BEGIN|[1]|Aggregations:0|SELECT Id, Name FROM Contact LIMIT 10
15:55:57.3 (16328398)|SOQL_EXECUTE_END|[1]|Rows:10
15:55:57.3 (16370589)|HEAP_ALLOCATE|[1]|Bytes:44
15:55:57.3 (16405300)|HEAP_ALLOCATE|[1]|Bytes:482
15:55:57.3 (16471210)|HEAP_ALLOCATE|[1]|Bytes:44
15:55:57.3 (16573998)|VARIABLE_ASSIGNMENT|[1]|contacts|{"s":1,"v":"List of size 10 too large to display"}|0x659ee34a
15:55:57.3 (16581759)|STATEMENT_EXECUTE|[2]
15:55:57.3 (16688191)|USER_DEBUG|[2]|DEBUG|1
15:55:57.3 (16714562)|STATEMENT_EXECUTE|[3]
15:55:57.3 (16787025)|SYSTEM_MODE_ENTER|false
15:55:57.3 (16830081)|HEAP_ALLOCATE|[3]|Bytes:5
15:55:57.3 (16922176)|HEAP_ALLOCATE|[3]|Bytes:24
15:55:57.3 (16945204)|SYSTEM_METHOD_ENTRY|[1]|JSON.JSON()
15:55:57.3 (16949217)|STATEMENT_EXECUTE|[1]
15:55:57.3 (16964027)|SYSTEM_METHOD_EXIT|[1]|JSON
15:55:57.3 (16987511)|METHOD_ENTRY|[3]||System.JSON.createGenerator(Boolean)
15:55:57.3 (17213905)|METHOD_EXIT|[3]||System.JSON.createGenerator(Boolean)
15:55:57.3 (17225995)|SYSTEM_MODE_EXIT|false
15:55:57.3 (17283165)|VARIABLE_ASSIGNMENT|[3]|generator|{"delegate":"0x689afdca"}|0x6d6cf6df
15:55:57.3 (17289080)|STATEMENT_EXECUTE|[4]
15:55:57.3 (17323339)|USER_DEBUG|[4]|DEBUG|2
15:55:57.3 (17332056)|STATEMENT_EXECUTE|[5]
15:55:57.3 (17367159)|SYSTEM_MODE_ENTER|false
15:55:57.3 (17385105)|HEAP_ALLOCATE|[5]|Bytes:5
15:55:57.3 (17419878)|METHOD_ENTRY|[5]||System.JSONGenerator.writeStartObject()
15:55:57.3 (17476900)|METHOD_EXIT|[5]||System.JSONGenerator.writeStartObject()
15:55:57.3 (17483710)|SYSTEM_MODE_EXIT|false
15:55:57.3 (17489625)|STATEMENT_EXECUTE|[6]
15:55:57.3 (17497217)|HEAP_ALLOCATE|[6]|Bytes:5
15:55:57.3 (17630176)|SYSTEM_MODE_ENTER|false
15:55:57.3 (17647850)|HEAP_ALLOCATE|[6]|Bytes:5
15:55:57.3 (17675833)|METHOD_ENTRY|[6]||System.JSONGenerator.writeNumberField(String, Integer)
15:55:57.3 (17759855)|METHOD_EXIT|[6]||System.JSONGenerator.writeNumberField(String, Integer)
15:55:57.3 (17773068)|SYSTEM_MODE_EXIT|false
15:55:57.3 (17782624)|STATEMENT_EXECUTE|[7]
15:55:57.3 (17819487)|SYSTEM_MODE_ENTER|false
15:55:57.3 (17847720)|HEAP_ALLOCATE|[7]|Bytes:5
15:55:57.3 (17863347)|METHOD_ENTRY|[7]||System.JSONGenerator.writeEndObject()
15:55:57.3 (17905570)|METHOD_EXIT|[7]||System.JSONGenerator.writeEndObject()
15:55:57.3 (17912042)|SYSTEM_MODE_EXIT|false
15:55:57.3 (17917614)|STATEMENT_EXECUTE|[8]
15:55:57.3 (17947036)|SYSTEM_MODE_ENTER|false
15:55:57.3 (17963621)|HEAP_ALLOCATE|[8]|Bytes:5
15:55:57.3 (17977339)|METHOD_ENTRY|[8]||System.JSONGenerator.getAsString()
15:55:57.3 (18025116)|METHOD_EXIT|[8]||System.JSONGenerator.getAsString()
15:55:57.3 (18033027)|SYSTEM_MODE_EXIT|false
15:55:57.3 (18058435)|VARIABLE_ASSIGNMENT|[8]|jsonString|"{\n  \"count\" : 10\n}"
15:55:57.3 (18062706)|STATEMENT_EXECUTE|[9]
15:55:57.3 (18068031)|HEAP_ALLOCATE|[9]|Bytes:12
15:55:57.3 (18094783)|HEAP_ALLOCATE|[9]|Bytes:30
15:55:57.3 (18123165)|USER_DEBUG|[9]|DEBUG|jsonString: {
  "count" : 10
}
15:55:57.18 (18187021)|CUMULATIVE_LIMIT_USAGE
15:55:57.18 (18187021)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 10 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

15:55:57.18 (18187021)|CUMULATIVE_LIMIT_USAGE_END

15:55:57.3 (18236374)|CODE_UNIT_FINISHED|execute_anonymous_apex
15:55:57.3 (20429513)|EXECUTION_FINISHED

 
David Lee 8David Lee 8
Is there a package I need to install or something like that? JSON is a part of System namespace, so I expect it to be avaialable by default.
Magesh Mani YadavMagesh Mani Yadav
Yes it available by default.
Did you really try the code which I shared because in your code you were missing some syntax.
David Lee 8David Lee 8
Yes, I pasted the whole thing.
Magesh Mani YadavMagesh Mani Yadav
can you share that console code again?
David Lee 8David Lee 8
I just noticed that my debug levels were messed up, that's why the messages did not show up. Thanks for your help!