Hey fellow salesforcers,
Im a bit stuck parsing specific values from a json into an apex List stringList
Using apex (with or without using Json2Apex), how can we parse the following JSON structure to construct a List that contains all of the SpecialName values?
Im hoping to use apex to parse the json into a List<String> stringList that i expect to contain all the SpecialNames , for example: {'valueOfInterest1','valueOfInterest2','valueOfInterest3',''... etc} however my stringList debugs to null
My JSON ( response variable ) : (with or without surrounding [])
{
"size" : 25,
"totalSize" : 25,
"done" : true,
"queryLocator" : null,
"entityTypeName" : "someEntityType",
"records" : [ {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/1"
},
"SpecialName" : "valueOfInterest1"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/2"
},
"SpecialName" : "valueOfInterest2"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/3"
},
"SpecialName" : "valueOfInterest3"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/4"
},
"SpecialName" : "valueOfInterest4"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/5"
},
"SpecialName" : "valueOfInterest5"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/6"
},
"SpecialName" : "valueOfInterest6"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/7"
},
"SpecialName" : "valueOfInterest7"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/8"
},
"SpecialName" : "valueOfInterest8"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/9"
},
"SpecialName" : "valueOfInterest9"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/10"
},
"SpecialName" : "valueOfInterest10"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/11"
},
"SpecialName" : "valueOfInterest11"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/12"
},
"SpecialName" : "valueOfInterest12"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/13"
},
"SpecialName" : "valueOfInterest13"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/14"
},
"SpecialName" : "valueOfInterest14"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/15"
},
"SpecialName" : "valueOfInterest15"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/16"
},
"SpecialName" : "valueOfInterest16"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/17"
},
"SpecialName" : "valueOfInterest17"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/18"
},
"SpecialName" : "valueOfInterest18"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/19"
},
"SpecialName" : "valueOfInterest19"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/20"
},
"SpecialName" : "valueOfInterest20"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/21"
},
"SpecialName" : "valueOfInterest21"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/22"
},
"SpecialName" : "valueOfInterest22"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/23"
},
"SpecialName" : "valueOfInterest23"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/24"
},
"SpecialName" : "valueOfInterest24"
}, {
"attributes" : {
"type" : "someEntityType",
"url" : "/some/path/25"
},
"SpecialName" : "valueOfInterest25"
} ]
}
Attempt: Trying to follow similar pattern to https://salesforce.stackexchange.com/a/201411/10073 by manually constructing the apex class, (not using json2apex):
public class MyPayload {
public class Result {
public final String SpecialName;
}
}
Problem: i get a null list of strings when i inspect stringList
List<String> stringList = new List<String>();
List<MyPayload.Result> results = (List<MyPayload.Result>)JSON.deserialize(
response, List<MyPayload.Result>.class
);
for (MyPayload.Result result : results)
{
stringList.add(result.SpecialName);
}
i must be parsing this incorrectly. Where am i going wrong and how do i populate stringList with all SpecialName values?