[Solved] How do I set API Headers?

Hi

Im new to this, but im basically trying to compile an app to help make my sons life easier after being diagnosed with Crohns.
Im trying to scan a food barcode, check this with the API Nutritionix - Nutrition Database API Documentation (msilverman) | RapidAPI and then highlight ingredients that are unsafe for him… but im falling down at the first hurdle with a null response, blocks are as below

Any help massively appreciated

Hello @matt_toyntond361, Welcome to our Community!

Try this,

The API Headers need to be in object type, you need to append ?upc= in the URL and remove .json at the end. Try making an API call using the above blocks, and share the delimited list you see, so we can help you get the right attribute you want from the list of response properties of the API.

You can read some more stuff about handling APIs in Thunkable here -

Let me know if this helps. Cheers!

1 Like

Thanks for this , blocks now as below

returned result is a comma delim list of numbers 0, - 59

how can i see the full response

@matt_toyntond361 - which property do you want to use from the JSON response?

Is something like this resource from @drted useful in understanding how to work with your data?

thanks again, i just really want to get to a position where im pulling barcode lookup in, then i can refine - looks like a v2 of the api is available so ive modified slightly but dont pull in what i was expecting it to - not too sure what im doing incorrectly here

Schoolboy error, typod on the URL , its now pulling in some info so just need to work out how to just disply ingredients ang highlight those know to cause issues for Chrohns

1 Like

Alright. Can you do one thing, instead of displaying response in a label, put it into a text input, copy the JSON response you’re getting, put it into https://jsonformatter.curiousconcept.com/, and share the pic of how the formatted JSON looks? Would help us get a better idea of where the ingredients property is located.

(or you can just share the raw json text here, i’ll format it)

Thanks, Kartik

1 Like

Full json being held for review but these are the fields that i needs to capture, picture is least important


Ok i think i have it working now with an alternative URL and product , ive marked the below with ** for the fields i want to capture, photo less important but would be nice for reference

{
“foods”:[
{
** “food_name”:“Rice Krispies”,
** “brand_name”:“Kellogg’s”,
“serving_qty”:1,
“serving_unit”:“serving”,
“serving_weight_grams”:30,
“nf_metric_qty”:30,
“nf_metric_uom”:“g”,
“nf_calories”:118.2300033569336,
“nf_total_fat”:0.4000000059604645,
“nf_saturated_fat”:0.10000000149011612,
“nf_cholesterol”:null,
“nf_sodium”:120,
“nf_total_carbohydrate”:26,
“nf_dietary_fiber”:0.8999999761581421,
“nf_sugars”:2.4000000953674316,
“nf_protein”:2.0999999046325684,
“nf_potassium”:null,
“nf_p”:null,
“full_nutrients”:[
{
“attr_id”:203,
“value”:2.1
},
{
“attr_id”:204,
“value”:0.4
},
{
“attr_id”:205,
“value”:26
},
{
“attr_id”:208,
“value”:118.23
},
{
“attr_id”:269,
“value”:2.4
},
{
“attr_id”:291,
“value”:0.9
},
{
“attr_id”:307,
“value”:120
},
{
“attr_id”:606,
“value”:0.1
}
],
“nix_brand_name”:“Kellogg’s”,
“nix_brand_id”:“51db37b0176fe9790a8983d8”,
“nix_item_name”:“Rice Krispies”,
“nix_item_id”:“5755d1df4af43853405196a5”,
“metadata”:{

  },
  "source":8,
  "ndb_no":null,
  "tags":null,
  "alt_measures":null,
  "lat":null,
  "lng":null,

** “photo”:{
“thumb”:“https://nutritionix-api.s3.amazonaws.com/60cb54d3bc476c0006962fc1.jpeg”,
“highres”:null,
“is_user_uploaded”:false
},
“note”:null,
“class_code”:null,
“brick_code”:null,
“tag_id”:null,
“updated_at”:“2021-06-17T13:57:39+00:00”,
** “nf_ingredient_statement”:“rice, sugar, contains 2% or less of salt, malt flavoring, bht for freshness. vitamins and minerals: iron, vitamin c (ascorbic acid), vitamin e (alpha tocopherol acetate), niacinamide, vitamin a palmitate, vitamin b6 (pyridoxine hydrochloride), vitamin b2 (riboflavin), vitamin b1 (thiamin hydrochloride), folic acid, vitamin b12, vitamin d.”
}
]
}

1 Like

Glad you got it working. Please mark the post you found helpful as " :white_check_mark: Solution" to mark this topic solved.

Good Luck on your app!
Cheers, Kartik.

THanks for all your help

hi Kartik

Im sorry i dont suppose you can guide me through the API queries can you im sure ive done everything correctly but i just get a value of null returned… or should i create a new post ?

image

There are two problems with how you are trying to parse the JSON response:

  1. When you get a property of an object, it will return a text string (so “food_name” will return “Rice Krispies”, for example). You should not then convert that text string back to JSON. That will just add unnecessary symbols such as { } to your result. So remove the generate JSON from object block.

  2. The square brackets [ ] in a JSON response indicate an array (list):

{
“foods”:[
{

So you’ll need to use List blocks to access that data or use property notation that includes list indexing. You also need to include the top property in the JSON path. So for example, to get “Rice Krispies” as a result, you’d need to get the property “foods[1].food_name” where “foods” is the top property name and [1] is the list index for the first item in the “foods” array.

If your “foods” list contains more than one item, then you’d need to reference additional items using their index value (such as “foods[2].food_name”) or use a loop to loop through them with the loop counter ‘j’ in place of the integer [2].

In case you’re interested, I have a video that explains JSON parsing here: API JSON Tutorial (Video)

1 Like

THanks, yes ill watch that now as im a little confused :slight_smile:

I don’t talk about property notation in the video but I do mention it in the comments in that topic.

Tatang, very informative thanks so much, im not sure if im being dense but im still not getting a value, in the JSONformatter your using i see it breaks each array down for you, im not seeing that in mine and am struggling to understand which properties to pull…

Based on your video ive changed the blocks as below, and im sure that the error is the properties im selecting, would you mind having a look over that ?


and this is the JSON after the URL and barcode are submitted

{
  "foods": [
    {
      "food_name": "Rice Krispies",
      "brand_name": "Kellogg's",
      "serving_qty": 1,
      "serving_unit": "serving",
      "serving_weight_grams": 30,
      "nf_metric_qty": 30,
      "nf_metric_uom": "g",
      "nf_calories": 118.2300033569336,
      "nf_total_fat": 0.4000000059604645,
      "nf_saturated_fat": 0.10000000149011612,
      "nf_cholesterol": null,
      "nf_sodium": 120,
      "nf_total_carbohydrate": 26,
      "nf_dietary_fiber": 0.8999999761581421,
      "nf_sugars": 2.4000000953674316,
      "nf_protein": 2.0999999046325684,
      "nf_potassium": null,
      "nf_p": null,
      "full_nutrients": [
        {
          "attr_id": 203,
          "value": 2.1
        },
        {
          "attr_id": 204,
          "value": 0.4
        },
        {
          "attr_id": 205,
          "value": 26
        },
        {
          "attr_id": 208,
          "value": 118.23
        },
        {
          "attr_id": 269,
          "value": 2.4
        },
        {
          "attr_id": 291,
          "value": 0.9
        },
        {
          "attr_id": 307,
          "value": 120
        },
        {
          "attr_id": 606,
          "value": 0.1
        }
      ],
      "nix_brand_name": "Kellogg's",
      "nix_brand_id": "51db37b0176fe9790a8983d8",
      "nix_item_name": "Rice Krispies",
      "nix_item_id": "5755d1df4af43853405196a5",
      "metadata": {},
      "source": 8,
      "ndb_no": null,
      "tags": null,
      "alt_measures": null,
      "lat": null,
      "lng": null,
      "photo": {
        "thumb": "https://nutritionix-api.s3.amazonaws.com/60cb54d3bc476c0006962fc1.jpeg",
        "highres": null,
        "is_user_uploaded": false
      },
      "note": null,
      "class_code": null,
      "brick_code": null,
      "tag_id": null,
      "updated_at": "2021-06-17T13:57:39+00:00",
      "nf_ingredient_statement": "rice, sugar, contains 2% or less of salt, malt flavoring, bht for freshness. vitamins and minerals: iron, vitamin c (ascorbic acid), vitamin e (alpha tocopherol acetate), niacinamide, vitamin a palmitate, vitamin b6 (pyridoxine hydrochloride), vitamin b2 (riboflavin), vitamin b1 (thiamin hydrochloride), folic acid, vitamin b12, vitamin d."
    }
  ]
}

I formatted your JSON response text above using the </> button in the forums toolbar. Without that, it’s not valid JSON because the forums convert quotes to smart quotes and that breaks the formatting.

After doing that and pasting it into Best JSON Viewer and JSON Beautifier Online, I get this:

image

You can see that when I click on a property such as “food_name”, it displays the path to that property at the top. In this case, it’s object → foods → 0 → food_name. The 0 is the first item of the JSON array/list (0, 1, 2, 3, etc.). In Thunkable, lists start at 1 (1, 2, 3, etc.). So as I said, we use a List block or property notation. I find property notation much easier because it’s a single text string for any property in the JSON response. But if you prefer to use List blocks, those will work as well.

The issue in your screenshot is that you’re taking the first list item (#1) of the property “nf_ingredient_statement” but if I click on that property on the JSON formatting website, the path to it is:

image

So that tells me that “foods” is the list, not “nf_ingredient_statement.” Therefore, you have to take the first list item of the property “foods” and not take the list item of “nf_ingredient_statement”. If “nf_ingredient_statement” was an array/list, it would have a list size like [1] or [5] after it on that website but it doesn’t. So it’s not a list.

I would encourage you to try using property notation as I find it so much more efficient. You would just get the property “foods[1].nf_ingredient_statement” of object app variable $API and be done with it. No need for List blocks or multiple get property of object blocks.

If that doesn’t work for you, post an updated screenshot. I can provide a screenshot as well… I’m just much faster sometimes at typing an explanation than creating a screenshot!

thanks for such a thorough explanation, with that and the right JSON formatter i think i get the idea of property notation, i do still however get the same null response…

Not that this is what i need but just to test that this does get all the information fine, and it does

You’re right… your blocks are correct are far as I can tell and when I do the same with the JSON response you posted above, I also get a result of null. It’s puzzling…

This is working:

So that leads me to think it was either a corrupt variable name (maybe the $ ?) or an invalid property value although I double-checked that text string.

Edit: and this returns null:

I’ve never had this happen with variables while parsing JSON. So I’m baffled!

@matt_conroy, any sense of what I’ve done wrong or perhaps discovered a new bug? Here’s the project link (see the first screen): Thunkable

I even tried really simple JSON:

{"name":"Amy", "age":30, "car":"Honda"}

And it still fails with a variable when getting the property “car” from the object but works when the JSON response is a text string connected to the get object from JSON block. Is it possible the initialize variable block is converting the format of the JSON incorrectly? As I said, I’ve never had this problem before with the many API responses I’ve parsed.