You need to sign in to do that
Don't have an account?
Jim Field
Error Parsing JSON array in Apex
Hello,
I am trying to parse the following JSON into a List of a custom Apex class, but I am getting this error: 1:55:17.0 (5766922)|USER_DEBUG|[210]|DEBUG|ERROR: System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set.
The JSON is:
{
"DCLPricing" : [ {
"PriceListNumber" : "C1000004",
"Currency" : "USD",
"CustomerNumber" : "C1000004",
"ValidFrom" : 20200101,
"ValidTo" : 20211231,
"Name" : "3M CORPORATION"
}, {
"PriceListNumber" : "C1000011",
"Currency" : "EUR",
"CustomerNumber" : "C1000011",
"ValidFrom" : 20200101,
"ValidTo" : 20211231,
"Name" : "A SCHULMAN PLAS"
}, {
"PriceListNumber" : "C1000022",
"Currency" : "USD",
"CustomerNumber" : "C1000022",
"ValidFrom" : 20200101,
"ValidTo" : 20211231,
"Name" : "A. SCHULMAN CO."
} ]
}
The code to parse the JSON is:
List<DCLPricing> priceLists = (List<DCLPricing>)JSON.deserialize(request.requestBody.tostring(), List<DCLPricing>.class);
The custom class is:
public class DCLPricing {
public String PriceListNumber;
public String CurrencyCode;
public String CustomerNumber;
public Date ValidFrom;
public Date ValidTo;
public String Name;
public DCLPricing(String priceListNumber, String currencyCode, String customerNumber, String validFrom, String validTo, String name ){
this.PriceListNumber = priceListNumber;
this.CurrencyCode = currencyCode;
this.CustomerNumber = customerNumber;
String year = validFrom.substring(0, 4);
String month = validFrom.substring(4,6);
String day = validFrom.substring(6, 8);
System.debug('year: ' + year + ' / ' + 'month: ' + month + ' / ' + 'day: ' + day);
this.ValidFrom = Date.parse(day + '/' + month + '/' + year);
year = validTo.substring(0, 4);
month = validTo.substring(4,6);
day = validTo.substring(6, 8);
System.debug('year: ' + year + ' / ' + 'month: ' + month + ' / ' + 'day: ' + day);
this.ValidTo = Date.parse(day + '/' + month + '/' + year);
this.Name = name;
}
}
Can anyone help me successfully parse this JSON into a list?
I am trying to parse the following JSON into a List of a custom Apex class, but I am getting this error: 1:55:17.0 (5766922)|USER_DEBUG|[210]|DEBUG|ERROR: System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set.
The JSON is:
{
"DCLPricing" : [ {
"PriceListNumber" : "C1000004",
"Currency" : "USD",
"CustomerNumber" : "C1000004",
"ValidFrom" : 20200101,
"ValidTo" : 20211231,
"Name" : "3M CORPORATION"
}, {
"PriceListNumber" : "C1000011",
"Currency" : "EUR",
"CustomerNumber" : "C1000011",
"ValidFrom" : 20200101,
"ValidTo" : 20211231,
"Name" : "A SCHULMAN PLAS"
}, {
"PriceListNumber" : "C1000022",
"Currency" : "USD",
"CustomerNumber" : "C1000022",
"ValidFrom" : 20200101,
"ValidTo" : 20211231,
"Name" : "A. SCHULMAN CO."
} ]
}
The code to parse the JSON is:
List<DCLPricing> priceLists = (List<DCLPricing>)JSON.deserialize(request.requestBody.tostring(), List<DCLPricing>.class);
The custom class is:
public class DCLPricing {
public String PriceListNumber;
public String CurrencyCode;
public String CustomerNumber;
public Date ValidFrom;
public Date ValidTo;
public String Name;
public DCLPricing(String priceListNumber, String currencyCode, String customerNumber, String validFrom, String validTo, String name ){
this.PriceListNumber = priceListNumber;
this.CurrencyCode = currencyCode;
this.CustomerNumber = customerNumber;
String year = validFrom.substring(0, 4);
String month = validFrom.substring(4,6);
String day = validFrom.substring(6, 8);
System.debug('year: ' + year + ' / ' + 'month: ' + month + ' / ' + 'day: ' + day);
this.ValidFrom = Date.parse(day + '/' + month + '/' + year);
year = validTo.substring(0, 4);
month = validTo.substring(4,6);
day = validTo.substring(6, 8);
System.debug('year: ' + year + ' / ' + 'month: ' + month + ' / ' + 'day: ' + day);
this.ValidTo = Date.parse(day + '/' + month + '/' + year);
this.Name = name;
}
}
Can anyone help me successfully parse this JSON into a list?
Try the below class:
Thanks,
Maharajan.C