I pulled a set of data from [Build, Collaborate & Integrate APIs | SwaggerHub](Food data central API) which returns in strings and numbers.
The following javascripts works fine when the data was reading a string of text.
// Parse the JSON string
const jsonString = p1;
const javascriptObject = JSON.parse(jsonString);
// Access the "title" property of the object using the provided key
const title = javascriptObject.foods[p2]?.foodNutrients[p2]?.nutrientName;
// Return the title, or a default value if it is not found
return title || "Invalid";
But when it comes to reading amount, it was unable to return the value.
Here’s is the API results.
"foods": [
{
"fdcId": 45001529,
"dataType": "Branded",
"description": "BROCCOLI",
"foodCode": "string",
"foodNutrients": [
{
"number": 303,
"name": "Iron, Fe",
"amount": 0.53,
"unitName": "mg",
"derivationCode": "LCCD",
"derivationDescription": "Calculated from a daily value percentage per serving size measure"
}
],
I tried to ParseFloat but it didn’t work out. Any coders here?