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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.