You need to sign in to do that
Don't have an account?

Please help with JSON
Hello All,
I'm brand new to this and I'm having trouble with generating JSON.
I am getting an error like "Cannot start array, expecting field name"
Please help!!!!
Here is what I'm trying to emulate:
{
"RatingZip": "",
"YearsInBusiness": "",
"NatureOfBusiness": "",
"IsNonProfit": "",
"Product": "", REQUIRED
"IsIncumbantAgent": "",
"ClassCodeList": [
{
"ClassCode": "", REQUIRED
"ClassDescriptionCode": "",
"LocState": "", REQUIRED
"Payroll": "", REQUIRED
"FullTimeEmployees": "",
"PartTimeEmployees": ""
}
],
"GenerateProposal": "",
"LegalEntity": "",
"Fein": "",
"Applicant": { REQUIRED
"BusinessName": "", REQUIRED
"BusinessAddress1": "", REQUIRED
"BusinessCity": "", REQUIRED
"BusinessState": "", REQUIRED
"BusinessZip": "", REQUIRED
"MailingAddress1": "",
"MailingCity": "",
"MailingState": "",
"MailingZip": "",
"ContactInformation": { REQUIRED
"FirstName": "", REQUIRED
"LastName": "", REQUIRED
"Email": "", REQUIRED
"Phone": "" REQUIRED
}
},
"Salutation": "",
"CompanyWebsiteAddress": "",
"EffectiveDate": "", REQUIRED
"ExpiredPremium": ""
}
And here is my code:
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('RatingZip', '');
gen.writeNumberField('YearsInBusiness', 0);
gen.writeStringField('NatureOfBusiness', '');
gen.writeBooleanField('IsNonProfit', false);
gen.writeStringField('Product', 'WC');
gen.writeBooleanField('IsIncumbantAgent', false);
gen.writeStringField('ClassCodeList', '');
gen.writeStartArray();
gen.writeStringField('ClassCode', i.Class_Code__c);
gen.writeStringField('ClassDescriptionCode', '');
gen.writeStringField('LocState', '');
gen.writeStringField('Payroll', i.Payroll__c);
gen.writeStringField('FullTimeEmployees', '');
gen.writeStringField('PartTimeEmployees', '');
gen.writeEndArray();
gen.writeBooleanField('GenerateProposal', true);
gen.writeNumberField('LegalEntity', 0);
gen.writeStringField('Fein', '');
gen.writeObjectField('Applicant', '');
gen.writeStartObject();
gen.writeStringField('BusinessName', i.Business_Name__c);
gen.writeStringField('BusinessAddress1', i.Business_Address__c);
gen.writeStringField('BusinessCity', i.Business_City__c);
gen.writeStringField('BusinessState', i.Business_State__c);
gen.writeStringField('BusinessZip', i.Business_Zip__c);
gen.writeStringField('MailingAddress1', '');
gen.writeStringField('MailingCity', '');
gen.writeStringField('MailingState', '');
gen.writeStringField('MailingZip', '');
gen.writeStartObject();
gen.writeObjectField('ContactInformation', '');
gen.writeStringField('FirstName', i.First_Name__c);
gen.writeStringField('LastName', i.Last_Name__c);
gen.writeStringField('Email', i.Email__c);
gen.writeStringField('Phone', i.Phone__c);
gen.writeEndObject();
gen.writeEndObject();
gen.writeStringField('Salutation', '');
gen.writeStringField('CompanyWebsiteAddress', '');
gen.writeStringField('EffectiveDate', i.Effective_Date__c);
gen.writeNumberField('ExpiredPremium', 0);
gen.writeEndObject();
I'm brand new to this and I'm having trouble with generating JSON.
I am getting an error like "Cannot start array, expecting field name"
Please help!!!!
Here is what I'm trying to emulate:
{
"RatingZip": "",
"YearsInBusiness": "",
"NatureOfBusiness": "",
"IsNonProfit": "",
"Product": "", REQUIRED
"IsIncumbantAgent": "",
"ClassCodeList": [
{
"ClassCode": "", REQUIRED
"ClassDescriptionCode": "",
"LocState": "", REQUIRED
"Payroll": "", REQUIRED
"FullTimeEmployees": "",
"PartTimeEmployees": ""
}
],
"GenerateProposal": "",
"LegalEntity": "",
"Fein": "",
"Applicant": { REQUIRED
"BusinessName": "", REQUIRED
"BusinessAddress1": "", REQUIRED
"BusinessCity": "", REQUIRED
"BusinessState": "", REQUIRED
"BusinessZip": "", REQUIRED
"MailingAddress1": "",
"MailingCity": "",
"MailingState": "",
"MailingZip": "",
"ContactInformation": { REQUIRED
"FirstName": "", REQUIRED
"LastName": "", REQUIRED
"Email": "", REQUIRED
"Phone": "" REQUIRED
}
},
"Salutation": "",
"CompanyWebsiteAddress": "",
"EffectiveDate": "", REQUIRED
"ExpiredPremium": ""
}
And here is my code:
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('RatingZip', '');
gen.writeNumberField('YearsInBusiness', 0);
gen.writeStringField('NatureOfBusiness', '');
gen.writeBooleanField('IsNonProfit', false);
gen.writeStringField('Product', 'WC');
gen.writeBooleanField('IsIncumbantAgent', false);
gen.writeStringField('ClassCodeList', '');
gen.writeStartArray();
gen.writeStringField('ClassCode', i.Class_Code__c);
gen.writeStringField('ClassDescriptionCode', '');
gen.writeStringField('LocState', '');
gen.writeStringField('Payroll', i.Payroll__c);
gen.writeStringField('FullTimeEmployees', '');
gen.writeStringField('PartTimeEmployees', '');
gen.writeEndArray();
gen.writeBooleanField('GenerateProposal', true);
gen.writeNumberField('LegalEntity', 0);
gen.writeStringField('Fein', '');
gen.writeObjectField('Applicant', '');
gen.writeStartObject();
gen.writeStringField('BusinessName', i.Business_Name__c);
gen.writeStringField('BusinessAddress1', i.Business_Address__c);
gen.writeStringField('BusinessCity', i.Business_City__c);
gen.writeStringField('BusinessState', i.Business_State__c);
gen.writeStringField('BusinessZip', i.Business_Zip__c);
gen.writeStringField('MailingAddress1', '');
gen.writeStringField('MailingCity', '');
gen.writeStringField('MailingState', '');
gen.writeStringField('MailingZip', '');
gen.writeStartObject();
gen.writeObjectField('ContactInformation', '');
gen.writeStringField('FirstName', i.First_Name__c);
gen.writeStringField('LastName', i.Last_Name__c);
gen.writeStringField('Email', i.Email__c);
gen.writeStringField('Phone', i.Phone__c);
gen.writeEndObject();
gen.writeEndObject();
gen.writeStringField('Salutation', '');
gen.writeStringField('CompanyWebsiteAddress', '');
gen.writeStringField('EffectiveDate', i.Effective_Date__c);
gen.writeNumberField('ExpiredPremium', 0);
gen.writeEndObject();
All Answers