0

I am running into an issue getting values from my json payload.

The schema is as follows:

{
  "thank_you_message": "Спасибо, что нашли время на прохождение нашего опроса. Ваше мнение очень важно для нас.",
  "Survey_Choices__c": [
    {
      "attributes": {
        "type": "ВыборВопросаОпроса",
        "url": "/services/data/v49.0/sobjects/SurveyQuestionChoice/0Kct0000000KzFyCAK"
      },
      "Name": "Интернет",
      "SurveyVersionId": "0Kst0000000KyjzCAC",
      "DeveloperName": "c_0dc4ff3e_13c5_46dd_b3e7_b5e9d2af8679",
      "Id": "0Kct0000000KzFyCAK"
    },
    {
      "attributes": {
        "type": "ВыборВопросаОпроса",
        "url": "/services/data/v49.0/sobjects/SurveyQuestionChoice/0Kct0000000KzFzCAK"
      },
      "Name": "мыПереводим",
      "SurveyVersionId": "0Kst0000000KyjzCAC",
      "DeveloperName": "c_296e4586_c4c3_43aa_8b55_4bb2f643befd",
      "Id": "0Kct0000000KzFzCAK"
    }]
}

I need to loop through all Survey_Choices__c but am running into problems.

My attempted steps

  1. Deserialize the json payload:

Map<String, Object> surveyMap = (Map<String, Object>) JSON.deserializeUntyped(output);

  1. Get the survey choice objects (I wanted to set this up as a Map<String,Object>, but I get a conversion error must be List<Object>)

  2. Use the following code to loop through each key-value pair

 List<Object> surveyChoices = (List<Object>) surveyMap.get('Survey_Choices__c');
    Map<String, Object> test = new Map<String, Object>();
      for (Object s : surveyChoices) {
         test.putAll((Map<String, Object>) s);
       }
    System.debug(test);

The above code only prints out one json object in the array.

My question is how can I loop through the array of JSON objects and get the name value of each object.

5
  • That JSON is not valid, you're missing a closing ]. Was that intentional? Aug 3 '20 at 19:54
  • Also, where is the line that throws the error on the title of your post? Aug 3 '20 at 20:00
  • Hi @SebastianKessel I have added the closing ] Aug 3 '20 at 20:03
  • I get an error when trying to do something like this Map<String,Object> surveyChoices = (Map<String,Object>) surveyMap.get('Survey_Choices__c'); Aug 3 '20 at 20:04
  • See my answer below. That error is expected as Survey_Choices__c is not an object, but a list of objects Aug 3 '20 at 20:04
1

Firstly, it is list of objects in Survey_Choices__c and each object has key-value pair which is how you need to cast and create object. By creating just Map<String, Object>, you are replacing the previous object (Map<String, Object>) in every iteration and so you are seeing just 1 object.

You can iterate as below:

Map<String, Object> surveyMap = (Map<String, Object>) JSON.deserializeUntyped(output);
List<Object> surveyChoices = (List<Object>) surveyMap.get('Survey_Choices__c');
List<Map<String, Object>> test = new List<Map<String, Object>>();
for (Object s : surveyChoices) {
    test.add((Map<String, Object>) s);
}
System.debug(test);

for(Map<String, Object> item : test) {
    for(String key : item.keySet()) {
        System.debug(key + '__' + item.get(key));
    }
}
1
  • 1
    this worked. I appreate your response and will keep in mind for future use Aug 4 '20 at 1:04

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.