We help IT Professionals succeed at work.
Get Started

output correct JSON response

-Dman100-
-Dman100- asked
on
329 Views
Last Modified: 2016-02-17
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.
Comment
Watch Question
Senior Director of Technology
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE