6

I am trying to create some config for front end based on config objects. I have one custom object where I specify options that I need. Each option is separate custom object of this type. I want to het all the options of some group and make a JSON object to use it at front end to render, disable and so on different parts of UI and buttons.

I have tried something like this:

public static String newSubmissionConfig(){

    NewSubmissionConfig con = new NewSubmissionConfig();
    List<NewSubmissionConfigOption> optList = new List<NewSubmissionConfigOption> ();
    List<FH_List_of_values__C> options = [select Name__c, isActive__c, isRequired__c from options__C where group__c = 'NEW_SUB_CONF'];

    for (options__C opt:options){
        NewSubmissionConfigOption option = new NewSubmissionConfigOption();
        NewSubmissionConfigOptions o = new NewSubmissionConfigOptions();
        option.name = opt.Name__c;
        o.isActive = opt.isActive__c;
        o.isRequired = opt.isRequired__c;
        option.NewSubmissionConfigOptions = o;
        optList.add(option);
    }

    con.newSubmissionConfigOption = optList;


    return JSON.serialize(optList);
}

public class NewSubmissionConfig {
    List<NewSubmissionConfigOption> newSubmissionConfigOption {get;set;}    
}

public class NewSubmissionConfigOption {
    public String name {get;set;}
    public NewSubmissionConfigOptions newSubmissionConfigOptions {get;set;}
}

public class NewSubmissionConfigOptions{        
    public Boolean isActive {get;set;}
    public Boolean isRequired {get;set;}
}

As expected I get object in the following format Array with Arrays inside:

config[
name: option1, newSubmissionConfigOptions [isActive: true, isReuired: true]
name: option2, newSubmissionConfigOptions [isActive: true, isReuired: true]
]

but what I need is just one object:

config: {option1: {isActive: true, isRequired: true},
         option2: {isActive: true, isRequired: true}}

So I can access this config like: config.option1.isActive Now I'm converting this JSON to proper format in JavaScript, but would be nice to get proper object directly from backend.

option1, option2... is Name__c field from my config custom object. Is there are any way to create such JSON objects right in APEX code?

2 Answers 2

11

I suggest building and serializing nested maps for this:

Map<String, Object> m = new Map<String, Object>();
for (options__C opt:options) {
    m.put(opt.Name__c, new Map<String, Boolean>{
            'isActive' => opt.isActive__c,
            'isRequired' => opt.isRequired__c
            });
}
return JSON.serialize(m);
2
  • +1 Ah your example is better with use of Object!
    – Raul
    Commented Mar 27, 2017 at 11:35
  • @LeonidZadorozhnykh Map<String, NewSubmissionConfigOptions> would also work if you wanted to avoid the inner map initialization, too, to borrow from your code, since you already know the field names.
    – sfdcfox
    Commented Mar 27, 2017 at 21:20
3

Desired format cannot be achieved with standard wrapper structure.

Usually I use maps to serialize for unsupported structure. An example below:

Map<String, String> mapOption = new Map<String, String>();
Map<String, Boolean> mapProperty = new Map<String, Boolean>();

for(options__C opt: [SELECT Name__c, isActive__c, isRequired__c FROM 
    options__C WHERE 
    group__c = 'NEW_SUB_CONF']){

    // collecting property as JSON string
    String strJsonProperty = JSON.serialize(mapProperty = new Map<String, Boolean>{
        isActive => opt.isActive__c,
        isRequired => opt.isRequired__c
    });

    // form map of options
    mapOption.put(opt.Name__c, strJsonProperty);
}

// JSON in your format
return JSON.serialize(mapOption);

Here is an example to illustrate:

Map<String, String> mapOption = new Map<String, String>();
Map<String, Boolean> mapProperty = new Map<String, Boolean>();

// collecting property as JSON string
String strJsonProperty = JSON.serialize(mapProperty = new Map<String, Boolean>{
    'isActive' => true,
    'isRequired' => true
});

// form map of options
mapOption.put('option1', strJsonProperty);
mapOption.put('option2', strJsonProperty);

String finalOutput = JSON.serialize(mapOption);

System.debug(finalOutput);

Output:

{"option2":"{\"isRequired\":true,\"isActive\":true}","option1":"{\"isRequired\":true,\"isActive\":true}"}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .