0

I'm new to salesforce and I have to parse this JSON and store the values in variables. I have used json2apex which generated 2 classes for me. When I use system.debug I can see it's reading the JSON object, however I have no idea how to store this many variables it in List<> format I used this line to store them but it does not work: for(Meals m: retrievedMeals){ mealData.add(m.meals[]); }

Can somebody please give me help me to store these values?

public class Meals {
@AuraEnabled
public List<Meals> meals;

@AuraEnabled
public String idMeal;
@AuraEnabled
public String strMeal;
@AuraEnabled
public String strCategory;
@AuraEnabled
public String strArea;
@AuraEnabled
public String strInstructions;
@AuraEnabled
public String strMealThumb;

public static Meals parse(String json) {
return (Meals) System.JSON.deserialize(json, Meals.class);
    }
}


public with sharing class MealsRetriever {
private static final String MEAL_URL = 'https://www.themealdb.com/api/json/v1/1/search.php?s=';
private static final String GET = 'GET';

public static List<Meals> getMealData(){

    List<Meals> mealData = new List<Meals>();

    Http httpCallout = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(MEAL_URL);
    req.setMethod(GET);

    HttpResponse mealResponse = httpCallout.send(req);
    Meals mealParseResponse = Meals.parse(mealResponse.getBody());
    List<Meals> retrievedMeals = mealParseResponse.meals;
    
    /*this is the part where i try to store the values, but I dont know how
    for(Meals m: retrievedMeals){
       //Meals.idMeals = retrievedMeals.get('idMeal');
    }


    return mealData;
    }
}
2
  • Hello, I have read that, however I still can't figure it out how to store the values in a List<> Apr 9 at 16:48
  • 3
    It's not clear what you're trying to accomplish but it doesn't sound like it's related to parsing JSON. Suggest modifying your question and describing your objectives. Perhaps Intro to Apex Trailhead would be useful to you. Also, inlining your code in the question rather than linking to Pastebin is a little friendlier for readers.
    – identigral
    Apr 9 at 17:08
1

This code doesn't quite make sense, but I think what you are seeking to do is iterate over a list of deserialized JSON objects and collect the values of a specific property.

First of all, this declaration does not belong in your Meals class:

@AuraEnabled
public List<Meals> meals;

This data structure is not self-referential (it does not contain instances of itself), and the data point you are seeking to retrieve is a String. You therefore need a local variable typed as a List<String>.

You probably want something like this:

List<Meals> retrievedMeals = mealParseResponse.meals;
List<String> mealIds = new List<String>();
for (Meals m : retrievedMeals) {
    mealIds.add(m.idMeal);
}

The get() method is available only on sObjects, not Objects like this one.


Alternately, if what you're really trying to do is return a list of Meal records, the most natural way to parse this is to use two classes to represent the inner and outer structures:

public class Meals {
    public class Meal {
        @AuraEnabled
        public String idMeal;
        @AuraEnabled
        public String strMeal;
        @AuraEnabled
        public String strCategory;
        @AuraEnabled
        public String strArea;
        @AuraEnabled
        public String strInstructions;
        @AuraEnabled
        public String strMealThumb;
    }

    @AuraEnabled
    public List<Meal> meals;


    public static Meals parse(String json) {
        return (Meals) System.JSON.deserialize(json, Meals.class);
    }
}

Then, you can return a List<Meals.Meal> derived from the top-level key meals (note that Meal is an inner class here):

    HttpResponse mealResponse = httpCallout.send(req);
    Meals mealParseResponse = Meals.parse(mealResponse.getBody());
    return Meals.meals;
2
  • Hello David, thanks a lot for your reply. After your answer I now have the following code: Meals mealParseResponse = Meals.parse(mealResponse.getBody()); List<Meals> retrievedMeals = mealParseResponse.meals; List<String> mealIds = new List<String>(); for (Meals m : retrievedMeals) { mealIds.add(m.idMeal); } But the .parse() say 'incorrect signature: void parse(String) from the type List<Meals>' And also cant find the variable 'meals' but I declared it: 'List<Meals> meals = new List<Meals>();' Can you help me please? Apr 12 at 2:38
  • List<Meals> does not have a parse() method. Meals does. Meals has a property meals, typed as a List<Meals.Meal>, not a List<Meals>. Assuming you're doing something like what I proposed above.
    – David Reed
    Apr 12 at 15:10

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