0

I am trying to create a JSON payload in a VF controller to send to middleware. I tried to use JSONGenerator.writeStringField(String, String) but kept getting error: "Can not write a field name, expecting a value"

I found the following question and answer, so I decided to give up on writeStringField and try JSON.serialize on inner classes instead. Writing JSON using JSONGenerator with writeStringField()

Here is an example of the structure (XML) that I need to mimic/create:

<SalesPersons>
    <SalesPerson>
        <RecordType/>
        <SalesPersonID>531</SalesPersonID>
        <Status>A</Status>
    </SalesPerson>
    <SalesPerson>
        <RecordType/>
        <SalesPersonID>538</SalesPersonID>
        <Status>A</Status>
    </SalesPerson>
</SalesPersons>

Here are the classes I created:

public class SalesPersons{
    List<SalesPerson> SalesPersons = new List<SalesPerson>();
    public SalesPersons(){}

    public void addSalesPerson(SalesPerson sp){
        this.SalesPersons.add(sp);
    }
}
public class SalesPerson{
    String SalesPersonID;
    String SalesPersonType;
    String SubType;
    String SalesPersonName;

    public SalesPerson(User u){
        SalesPersonID = u.EmployeeNumber;
        SalesPersonType = 'S';
        SubType = 'P';
        SalesPersonName = u.Name;
    }
}

Here is the JSON Payload that gets created from this:

{
  "SalesPersons": [
    {
      "SubType": "P",
      "SalesPersonType": "S",
      "SalesPersonName": "SSO User",
      "SalesPersonID": "101"
    },
    {
      "SubType": "P",
      "SalesPersonType": "S",
      "SalesPersonName": "Admin User",
      "SalesPersonID": null
    }
  ]
}

Where I am stuck is on creating multiple arrays 'named' SalesPerson. The JSON that I am currently generating contains the info for multiple SalesPerson objects, but I need the String "SalesPerson" to be present in each.

  • 1
    If you want {"SalesPerson": {}, "SalesPerson": {}}, I'm pretty sure that is incompatible with JSON. Is that what you're looking for? – Adrian Larson May 18 '16 at 14:28
  • It is what I'm looking for unfortunately. I was able to create that using JSONGenerator.writeString(String), JSONGenerator.writeStartArray(), JSONGenerator.writeEndArray(). I do think that it's possible with inner classes, I have just not been successful with it yeyt. – Mimi Sakarett May 18 '16 at 14:33
  • Your XML and JSON both have many entries in a SalesPersons array (so many sales person items) which is fine. – Keith C May 18 '16 at 15:12
1

A quick hack of the http://json2apex.herokuapp.com/ output produces:

// You can call the outer and inner classes whatever you like: its the
// field names and types that matter
public class SalesPersons {

    public class SalesPerson {
        public String SubType;
        public String SalesPersonType;
        public String SalesPersonName;
        public String SalesPersonID;
    }

    public SalesPerson[] SalesPersons = new SalesPerson[] {};

    public static SalesPersons parse(String jsonString) {
        return (SalesPersons) JSON.parse(jsonString, SalesPersons.class);
    }

    public String serializePretty() {
        return JSON.serializePretty(this);
    }

    public void addSalesPerson(User u) {
        SalesPerson sp = new SalesPerson();
        sp.SalesPersonID = u.EmployeeNumber;
        sp.SalesPersonType = 'S';
        sp.SubType = 'P';
        sp.SalesPersonName = u.Name;
        SalesPersons.add(sp);
    }
}

Note that lists and arrays are interchangeable in this context.

To build the JSON:

SalesPersons sps = new SalesPersons();
for (User u : [select Name, EmployeeNumber from User limit 1000]) {
    sps.addSalesPersons(u);
}

String jsonString = sps.serializePretty();
  • Does it actually produce multiple elements with the same key? – Adrian Larson May 18 '16 at 15:46
  • @AdrianLarson Nope. It produces the JSON listed in the question which is the sane translation of the XML into JSON rather than what the OP is asking for which as you point out is not valid JSON. – Keith C May 18 '16 at 16:08
0

Try with this syntax, instead of using JSONGenerator.

SalesPersons spRecord = new SalesPersons();

//fill your object

string myJSON = JSON.serialize(spRecord);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.