0

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?

2

You have the right idea, only change needed would be with construction of the wrapper structure.

In this case, the type attribute in attributes json string is a reserved character in apex, workaround to get a simpler structure with json2apex is to replace type with type_x or something like that.

Manually constructing the wrapper or using json to apex would give an apex class as below:

public with sharing class PayloadHelper {

    public class MyPayload {
        public Integer size {get;set;}
        public Integer totalSize {get;set;}
        public Boolean done {get;set;}
        public Object queryLocator {get;set;}
        public String entityTypeName {get;set;}
        public List<Records> records {get;set;}
    }
    public class Attributes {
        // use the modified reserved keyword as property
        public String type_x {get;set;}
        public String url {get;set;}
    }
    public class Records {
        public Attributes attributes {get;set;}
        public String SpecialName {get;set;}
    }
    public static MyPayload parse(String json) {
        // modify the reserved keyword
        String modifiedJsonString  = json.replace('"type"', '"type_x"');
        return (MyPayload) System.JSON.deserialize(modifiedJsonString, MyPayload.class);
    }
}

Now the actual logic would look as below:

String jsonString = '{ your JSON in here }';
PayloadHelper.MyPayload objParsedWrapper = PayloadHelper.parse(jsonString);
List<String> stringList = new List<String>();
for(PayloadHelper.Records records: objParsedWrapper.records) {
    stringList.add(records.SpecialName);
}

Basically it iterates over the response to collect the desired data would output:

DEBUG|(valueOfInterest1, valueOfInterest2, valueOfInterest3, valueOfInterest4, valueOfInterest5, valueOfInterest6, valueOfInterest7, valueOfInterest8, valueOfInterest9, valueOfInterest10, ...)

1
  • Nice one, Raul! 🤟🏼 Aug 30 '20 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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