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
Vidhya K 14Vidhya K 14 

JsonGenerator class

Hi,

I'm trying to input value which is an array index inside JSONGenerator class. I'm not sure how to represent this. Can anyone help me on this.

Json syntax: "excludedDocuments": ["2"]

JSONGenerator: gen.writeStringField('excludedDocuments', ['2']);  

When I give this as array, it throws error. Is there any other format to be used? Please suggest.

Regards,
Vidhya
VamsiVamsi
Hi Vidhya,

Please make sure that you have a clear JSON syntax. Because for an array you need a key -> value pair and in the above question you just have a value.

Sample format 

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();     
gen.writeFieldName('excluded Documents');
gen.writeStartArray();
gen.writeStartObject(); 
gen.writeStringField('id','2');
gen.writeEndObject();
gen.writeEndArray();
gen.writeEndObject();
string n = gen.getAsString();
system.debug('gens string'+ n);

output 

gens string :
{
"excluded Documents" : [ { "id" : "2" } ]
}

hope this would be of any help..!!