How to Consume JSON Array in Apex REST API Class : Stuck with deserialization Error

The { starts an object and the [ starts an array. It is convenient to deserialise into inner classes (because the class field names must match the JSON names) and then transfer from those into the SObjects:

public class Item {
    public String sName;
    public String sRating;
    public String sContact;
}

@HttpPost
global static String createMerchandise(Item[] rTest) {
    Account[] accounts = new Account[] {};
    for (Item i : rTest) {
        accounts.add(new Account(Name = i.sName, ??? = i.sRating));
    }
    // Other logic relating to Contact?
}

The parameters of the method must match the root JSON object field names and types.

The platform does the deserialisation from JSON to Apex objects automatically if you use the above pattern.

update: fixed a typo in Account creation code

Tags:

Json

Apexrest