0

I'm trying to parse the JSONArray that has been stored in the Custom Metadata Types.

1.The JSONArray(Stored in Custom Metadata) looks like,

[{
    "time": "5 AM",
    "leads":[{
        "tz":["EST"],
        "fresh":["shift1"],
        "overnight":["shift1"]
    }]
}]

2.The apex to get the config,

SettingList = new List<Setting__mdt>([SELECT Id, MasterLabel, Distribution__c
                                  FROM Setting__mdt WHERE DeveloperName = 'Default']);

3.Trying to parse it in Apex,

//Need to parse as a List, because the value is JSONArray not JSONObject
List<Object> configs = JSON.deserializeUntyped(SettingList[0].Distribution__c);

Getting error: Illegal assignment from Object to List Please help me here to parse my JSONArray...

3
  • I don't think the question duplicates that one. Here is used untyped deserialization and there are cases where you are limited in choices in what to use. Nov 26 '21 at 8:16
  • 1
    @OleksandrBerehovskyi Thanks for your suggestion, I have tried it. it is working fine with JSON2Apex.
    – DillGates
    Nov 26 '21 at 8:46
  • 1
    @MariiaIllarionova untyped deserialization is used, but why not use a specific class to parse a JSON ?:) this question is a good example of X-Y problem and, per my understanding, community duty is to provide the most appropriate solution for the problem, even if OP author was not aware of the possibility to solve it in another way. Nov 26 '21 at 8:59
1

Just add casting to List<Object> before deserialization. Now you can iterate through your List. Deserializing inner-array of leads will be in the same way.

List<Object> configs = (List<Object>) JSON.deserializeUntyped(jsonString);
for (Object configObject : configs) {
    Map<String, Object> config = (Map<String, Object>) configObject;
    System.debug(config);
}

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