# Javascript unable to read numbers value

**URL:** <https://community.glideapps.com/t/javascript-unable-to-read-numbers-value/63516>\
**Category:** Ask for Help\
**Created:** [July 5, 2023, 6:14am UTC](https://community.glideapps.com/t/javascript-unable-to-read-numbers-value/63516 "2023-07-05T06:14:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trustin](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/trustin/32/88217_2.png) [@Trustin](https://community.glideapps.com/u/Trustin)\
**Post date:** [July 5, 2023, 6:14am UTC](https://community.glideapps.com/t/javascript-unable-to-read-numbers-value/63516/1 "2023-07-05T06:14:34Z")

</div>

I pulled a set of data from [[Build, Collaborate & Integrate APIs | SwaggerHub](https://app.swaggerhub.com/apis/fdcnal/food-data_central_api/1.0.1#/)](Food data central API) which returns in strings and numbers.  
The following javascripts works fine when the data was reading a string of text.

```auto
// 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.

```auto
"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?

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 5, 2023, 9:28am UTC](https://community.glideapps.com/t/javascript-unable-to-read-numbers-value/63516/2 "2023-07-05T09:28:45Z")

</div>

Works for me 🤷‍♂️

 ![CleanShot 2023-07-05 at 17.27.12@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/0/1087a2e25fcdcb89f30b310413eff2c8b78543b1.png)

```auto
const json = JSON.parse(p1);
return json.foods[0].foodNutrients[0].number;

```

```auto
const json = JSON.parse(p1);
return json.foods[0].foodNutrients[0].amount;

```

---

<div class="post-metadata">

**Author:** ![Trustin](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/trustin/32/88217_2.png) [@Trustin](https://community.glideapps.com/u/Trustin)\
**Post date:** [July 5, 2023, 10:18am UTC](https://community.glideapps.com/t/javascript-unable-to-read-numbers-value/63516/3 "2023-07-05T10:18:00Z")

</div>

The results was a lot longer, i tried with the above snippets of code i provided and got it to display the amount. But the picture below is reading from the full results, it gives a blank without an error…

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/4/1/41c8e4bf6c82a20f8226c7145074d04b5f981d08.png)

---

<div class="post-metadata">

**Author:** ![Trustin](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/trustin/32/88217_2.png) [@Trustin](https://community.glideapps.com/u/Trustin)\
**Post date:** [July 5, 2023, 12:16pm UTC](https://community.glideapps.com/t/javascript-unable-to-read-numbers-value/63516/4 "2023-07-05T12:16:54Z")

</div>

I may be calling the wrong item. instead of amount, i should call the value item.  
Anyhow, its working as I hope it will be, the 0 value returns Invalid but I implement the if statement to output the 0 value. Below is the code.

```auto
// 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]?.value;
if (title === 0) {
  return title.toString();
} else {
  return title || "Invalid";
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [July 6, 2023, 12:17pm UTC](https://community.glideapps.com/t/javascript-unable-to-read-numbers-value/63516/5 "2023-07-06T12:17:29Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
