output correct JSON response

I'm using apex language, which is similar to JAVA to generate some JSON output with the following code:

    generator = JSON.createGenerator(true);
    generator.writeStartObject();
    generator.writeFieldName('employees');
    generator.writeStartArray();
    for(User u : getUsers()) {
        generator.writeStartObject();
        generator.writeStringField('key', u.Id);
        generator.writeStringField('nickname', u.CommunityNickname);
        generator.writeStringField('lastname', u.LastName);
        if(u.Region__c != null) {
            generator.writeStringField('region', u.Region__c);
        }
        else {
            generator.writeStringField('region', '');
        }
        if(u.Home_District__c != null) {
            generator.writeStringField('district', u.Home_District__c);
        }
        else {
            generator.writeStringField('district', '');
        }
        if(u.Home_District__c != null) {
            generator.writeStringField('pack', u.Pack__c);
        }
        else {
            generator.writeStringField('pack', '');
        }
        generator.writeStringField('employee', u.Name);
        generator.writeEndObject();
    }
    generator.writeEndArray();
    generator.writeFieldName('events');
    generator.writeStartArray();
    for(Site_Visit__c sv : getSiteVisits()) {
        generator.writeStartObject();
        if(sv.Status__c.toLowercase() == 'scheduled') {
            generator.writeFieldName('assigned');
            generator.writeStartObject();
            if(sv.Intended_Start_Date_Time__c != null) generator.writeStringField('start_date', String.valueOf(sv.Intended_Start_Date_Time__c));    
            if(sv.Estimated_Completion_Date_Time__c != null) generator.writeStringField('end_date', String.valueOf(sv.Estimated_Completion_Date_Time__c));
            generator.writeStringField('text', String.valueOf(sv.Name));
            if(sv.Assigned_Employee__c != null) generator.writeStringField('section_id', String.valueOf(sv.Assigned_Employee__c));
            generator.writeStringField('id', String.valueOf(sv.Id));
            generator.writeStringField('color', String.valueOf('#ffffff'));
            generator.writeStringField('textColor', String.valueOf('#000000'));
            generator.writeEndObject(); 
        }
        if(sv.Status__c.toLowercase() == 'unassigned') {
            generator.writeFieldName('unassigned'); 
            generator.writeStartObject();
            if(sv.Intended_Start_Date_Time__c != null) generator.writeStringField('start_date', String.valueOf(sv.Intended_Start_Date_Time__c));    
            if(sv.Estimated_Completion_Date_Time__c != null) generator.writeStringField('end_date', String.valueOf(sv.Estimated_Completion_Date_Time__c));
            generator.writeStringField('text', String.valueOf(sv.Name));
            generator.writeStringField('id', String.valueOf(sv.Id));
            generator.writeStringField('color', String.valueOf('#ffffff'));
            generator.writeStringField('textColor', String.valueOf('#000000'));
            generator.writeStringField('woNumber', String.valueOf(sv.Work_Order__r.Name));
            generator.writeStringField('woType', String.valueOf(sv.Work_Order__r.Work_Order_Type__c));
            generator.writeStringField('customer', String.valueOf(sv.Work_Order__r.Customer__c));
            generator.writeEndObject();             
        }
        generator.writeEndObject(); 
    }
    generator.writeEndArray();
    generator.writeEndObject();

Open in new window


The current output is like the following:

{
employees:
    employee
    employee
events:
    unassigned:
        event
    unassigned:
        event
    assigned:
        event
    assigned:
        event
    (empty object?)
    (empty object?)
}

Open in new window


I need it to be like this:

{
employees:
    employee
    employee
events:
    unassigned:
        event
        event
    assigned:
        event
        event
}

Open in new window


Events should be an object (not an array) that has two fields: assigned and unassigned. Each of those fields is an array of events that match their description.  How can I modify the existing structure to get the correct output.

Thanks for any help.
-Dman100-Software ConsultantAsked:
Who is Participating?
 
Jeffrey Dake Senior Director of TechnologyCommented:
Have you considered when you loop through your
for(Site_Visit__c sv : getSiteVisits()) {

Open in new window

loop, making two lists.  One you populate with the assigned events, the other with the unassigned events.  After you gather all those, you can then loop through those and write those out with your generator.  I think that should work.  Hope that helps.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.