0

I'm trying to repeat the node items inside the JSON, the expected JSON output should be:

{
    "items": [{
        "email": "[email protected]",
        "fields": [{
            "name": "name",
            "value": "John Perez"
        }]
    }, {
        "email": "[email protected]",
        "fields": [{
            "name": "name",
            "value": "Johnny Sanchez"
        }]
    }],
    "fields": ["name"]
}

But what I get so far is:

{
  "items":[{
    "fields":[{
      "value":"string","name":"string"
    }],
    "email":"[email protected]"
  }],
    "fields":["name"]
},
{
  "items":[{
    "fields":[{
      "value":"string","name":"string"
    }],
    "email":"[email protected]"
  }],
    "fields":["name"]
}

Im not finding the way to repeat the {} inside items: [] instead the items is repeated on each JSON.

My apex code is the next:

public String name;
public String[] fields;
public cls_items[] items;
class cls_items {
    public cls_fields[] fields;
    public String email; //string
}
class cls_fields {
    public String name; //string
    public String value; //string
}
public static String listJSON {get;set;}
public static String upsertSubscribersJSON(List<DopplerServiceUtil.SubsInfo> SubsList) {
    DopplerJSONSerializer suscWrapper = new DopplerJSONSerializer();
    DopplerJSONSerializer.cls_items items = new DopplerJSONSerializer.cls_items();

    DopplerJSONSerializer.cls_fields fields = new DopplerJSONSerializer.cls_fields();
    listJSON = ''; // prevents 'null' at the begging

    // Building the JSON Model
    for (DopplerServiceUtil.SubsInfo subscriber : SubsList) {
        fields.value = subscriber.SubName;
        fields.name = 'name';

        if(subscriber.SubEmail != null) { items.email = subscriber.SubEmail; }
        else {items.email = 'empty';}
        items.fields = new List<DopplerJSONSerializer.cls_fields>{ fields };

        suscWrapper.fields = new List<String>{'name'};
        suscWrapper.items = new List<DopplerJSONSerializer.cls_items> {items};
        listJSON += JSON.serializePretty(suscWrapper, true)+',';
    }
    return listJSON;
}
3
  • Whats the JSON your trying to serialize? That first example isn't valid, and is there any reason you aren't passing the entire object to serialize?
    – EricSSH
    Mar 31, 2017 at 19:34
  • 1
    Put your expected results in json2apex.herokuapp.com - Problem solved
    – Eric
    Mar 31, 2017 at 19:39
  • Figured It should be a valid JSON before I did that
    – EricSSH
    Mar 31, 2017 at 19:43

1 Answer 1

2

You need to do some minor cleanup of your desired JSON before you can serialize anything into it. JSON Lint is quite helpful here:

"items": [
    {...},
    {...},
//       ^ remove trailing comma
]

Once you've got a valid payload, you can pass it to JSON2Apex, which provides a passable class for you to use. I would rename Fields to Field and Items to Item but it appears usable for your purposes:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public class Fields {
        public String name;
        public String value;
    }

    public class JSON2Apex {
        public List<String> fields;
        public List<Items> items;
    }

    public class Items {
        public String email;
        public List<Fields> fields;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

So the DTO I would actually use would look more like:

public class SubscriberInfo
{
    public List<String> fields;
    public List<Item> items;
    public SubscriberInfo()
    {
        fields = new List<String>();
        items = new List<Item>();
    }

    public class Item
    {
        public String email;
        public List<Field> fields;
        public Item()
        {
            fields = new List<Field>();
        }
    }
    public class Field
    {
        public String name, value;
        public Field(String name, String value)
        {
            this.name = name;
            this.value = value;
        }
    }
}

Then you would construct and populate it along the following lines:

SubscriberInfo info = new SubscriberInfo();
info.fields.add('name');

for (DopplerServiceUtil.SubsInfo subscriber : subscribers)
{
    SubscriberInfo.Item item = new SubscriberInfo.Item();
    item.fields.add(new SubscriberInfo.Field('name', subscriber.subName));
    if(subscriber.subEmail != null) { items.email = subscriber.subEmail; }
    info.items.add(item);
}
String payload = JSON.serialize(info);
1
  • Adrian, this was a master class for me of how to do data wrappers and build the json structure. Thank you
    – BoDiE2003
    Apr 3, 2017 at 15:19

You must log in to answer this question.

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