Using a JSON method to search REST API data based on the item Id rather than the item name

1k views Asked by At

I am using FatSecreet API and I am using two different methods. I want to use the Id of the first method item clicked as the item identifier. I am currently able to get the information based on the first methods "food_name" but I want to use the "food_id" to ensure 100 percent accuracy. Is it possible to search the API based on the items "food_id"? Documentation http://platform.fatsecret.com/api/Default.aspx?screen=rapih. If you need more info/code I will update my question. I would like to just know how I would use JSON statements in the method "food.get" based on the item "food_id" that I am getting from the first method "food.search".

First, to search the database I am using "foods.search" Which returns:

"foods.search" Example Return

{  
   "foods":{  
      "food":{  
         "food_description":"Per 342g - Calories: 835kcal | Fat: 32.28g | Carbs: 105.43g | Protein: 29.41g",
         "food_id":"4384",
         "food_name":"Plain French Toast",
         "food_type":"Generic",
         "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/french-toast-plain"
      },
      "max_results":"20",
      "page_number":"0",
      "total_results":"228"
   }
}

Second Method, I am using "foods.get" which returns:

"foods.get" Example Return

    {  
   "food":{  
      "food_id":"4384",
      "food_name":"Plain French Toast",
      "food_type":"Generic",
      "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/french-toast-plain",
      "servings":{  
         "serving":{  
            "calcium":"8",
            "calories":"159",
            "carbohydrate":"20.02",
            "cholesterol":"90",
            "fat":"6.13",
            "fiber":"0.8",
            "iron":"9",
            "measurement_description":"regular slice",
            "metric_serving_amount":"65.000",
            "metric_serving_unit":"g",
            "monounsaturated_fat":"2.298",
            "number_of_units":"1.000",
            "polyunsaturated_fat":"1.578",
            "potassium":"80",
            "protein":"5.58",
            "saturated_fat":"1.585",
            "serving_description":"regular slice",
            "serving_id":"16758",
            "serving_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/french-toast-plain?portionid=16758&portionamount=1.000",
            "sodium":"320",
            "sugar":"4.87",
            "trans_fat":"0",
            "vitamin_a":"0",
            "vitamin_c":"0"
         }
      }
   }
}

UPDATE I posted a working example on GitHub

https://github.com/EugeneHoran/Android-FatSecret-REST-API

1

There are 1 answers

0
Jordan Running On BEST ANSWER

Is it possible to search the API based on the items "food_id"?

Yes. The documentation for food.get says it requires a food_id parameter.

I would like to just know how I would use JSON statements in the method "food.get" based on the item "food_id" that I am getting from the first method "food.search".

It's really unclear what you're saying here—"JSON statements" doesn't make sense—but if we turned the JSON object that food.search responds with into a JavaScript object called response, we would access the food_id property with response.foods.food.food_id.