+ Start a Discussion
Andy Kallio 7Andy Kallio 7 

Admin needs advice on generating JSON: generator vs. serialize?

Hello friends. I am playing around with generating a nested json string to send to netsuite. It seems like there are two options: use the JSONGenerator or use JSON.serialize() and use it seralize a class that has properties that are the same as netsuite's. 

I have Generator working in the snippet below. This is my first time doing this kind of thing. So, I'm just wondering if there are any opinions on these two approaches  or maybe something I haven't considered. 
 
JSONGenerator gen = JSON.createGenerator(true);
Invoicing__c invs = [select Id, 
                           		  Name, 
                                  Invoice_Name__c, 
                                  Expected_Invoice_Date__c,
                                  Primary_Billing_Contact_Email_id__c,
                                  Project__r.Subsidiary_Usage__r.Netsuite_Subsidiary_Id__c,
                                  Project__r.Project_Consultant__r.Netsuite_Id__c,
                                  Project__r.Owner_Netsuite_Id__c,
                                  Status__c,
                                  Project__r.Netsuite_Id__c,
                                  (select Id, Quantity__c, Sales_Price__c, Product__r.Netsuite_Product_Code__c from Invoice_Line_Items__r)
                          from Invoicing__c limit 1];
system.debug(invs.Name);

gen.writeStartObject();
gen.writeStringField('recordType', 'invoice');
gen.writeStringField('isDynamic', 'false');
gen.writeFieldName('columns');
gen.writeStartObject();
gen.writeObjectField('tranid', invs.Name);
gen.writeObjectField('companyid', invs.Project__r.Netsuite_Id__c);
gen.writeObjectField('trandate', invs.Expected_Invoice_Date__c);
gen.writeEndObject();

gen.writeFieldName('lines');
gen.writeStartObject();
gen.writeStringField('sublistId', 'item');
gen.writeFieldName('lineItems');
gen.writeStartArray();
for(Invoice_Line_Item__c ili : invs.Invoice_Line_Items__r) {
	gen.writeStartObject();
	gen.writeObjectField('quantity', ili.Quantity__c);
	gen.writeObjectField('rate', ili.Sales_Price__c);
	gen.writeObjectField('accout', ili.Product__r.Netsuite_Product_Code__c);
	gen.writeEndObject();
}    
gen.writeEndArray();
gen.writeEndObject();
gen.writeEndObject();
    
system.debug(gen.getAsString());

 
Best Answer chosen by Andy Kallio 7
Ramesh DepaiahRamesh Depaiah

If you have any specific requirement regarding JSON structure then you need to go for JSON Generator. But if you are okay with the JSON syntax JSON.serialize() is giving, serialize is more than enough.
 
Invoicing__c invs = [select Id, 
                           		  Name, 
                                  Invoice_Name__c, 
                                  Expected_Invoice_Date__c,
                                  Primary_Billing_Contact_Email_id__c,
                                  Project__r.Subsidiary_Usage__r.Netsuite_Subsidiary_Id__c,
                                  Project__r.Project_Consultant__r.Netsuite_Id__c,
                                  Project__r.Owner_Netsuite_Id__c,
                                  Status__c,
                                  Project__r.Netsuite_Id__c,
                                  (select Id, Quantity__c, Sales_Price__c, Product__r.Netsuite_Product_Code__c from Invoice_Line_Items__r)
                          from Invoicing__c limit 1];
system.debug(JSON.serialize(invs))

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D

All Answers

Ramesh DepaiahRamesh Depaiah

If you have any specific requirement regarding JSON structure then you need to go for JSON Generator. But if you are okay with the JSON syntax JSON.serialize() is giving, serialize is more than enough.
 
Invoicing__c invs = [select Id, 
                           		  Name, 
                                  Invoice_Name__c, 
                                  Expected_Invoice_Date__c,
                                  Primary_Billing_Contact_Email_id__c,
                                  Project__r.Subsidiary_Usage__r.Netsuite_Subsidiary_Id__c,
                                  Project__r.Project_Consultant__r.Netsuite_Id__c,
                                  Project__r.Owner_Netsuite_Id__c,
                                  Status__c,
                                  Project__r.Netsuite_Id__c,
                                  (select Id, Quantity__c, Sales_Price__c, Product__r.Netsuite_Product_Code__c from Invoice_Line_Items__r)
                          from Invoicing__c limit 1];
system.debug(JSON.serialize(invs))

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
This was selected as the best answer
Andy Kallio 7Andy Kallio 7
Thanks. I bascially got the same understanding from the documentation but it's nice for me to have this validation...thanks for the taking the time. And, I think my case is clear that I have to use the generator because of the need to map the specfic field names in netsuite.