function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jim FieldJim 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?
 
Maharajan CMaharajan C
Hi Jim,

Try the below class:
 
public class DCLPricingList {
    public List<DCLPricing> DCLPricing;
    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;
        }
    }
}
 
//String str = '{"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."}]}';

String reqString = request.requestBody.tostring();
reqString = reqString.replace('Currency', 'CurrencyCode');
DCLPricingList priceLists = (DCLPricingList)JSON.deserialize(reqString , DCLPricingList.class);
system.debug('priceLists ==> ' +  priceLists );

Thanks,
Maharajan.C