How to create nutrition app with ocr scanner?

If the “error” block has a value, you generally shouldn’t proceed with the next steps because it will likely result in the app crashing or other unpredictable behavior.

In the tutorial video I made, I show how to use an if/then block to check that value.

But a simple way to check it is to assign the “error” block to a label’s text and see what the result is.


This is what it says in the error box:

Ohhh… you should live test it on your device

Okay thank you it worked on my phone! But when the barcode scanner opens on my phone, it goes onto a different camera but doesn’t let me take a picture or scan anything.

Yes it does. When you pass the camera over a barcode it automatically captures the barcode.

1 Like

Okay I’ll try again

I’m trying to get the nutriscore so I put in the positive and negative points but when I scan it just says “undefined”.

Why are you setting Main’s Text to app variable json response? You should be setting that label’s text to the “get property of object blocks.”

I was trying to mess around with the status numbers but I’m not sure which is the right number, so I can get the actual value. Right now I get the value “0”. So what numbers should I put in there? I’m trying to get the nutriscore which is like gthe food a grade (A,B,C,D,E). And I think that the positive and negative points will help me get that. So what do I need to change in order to get that the food grade?

This is what I have so far:

You can Google API status codes to learn more about them. A status of “200” is a successful connection to the API server. You should only be checking to see if status equals 200. If it doesn’t, then something went wrong and you probably have a value in the green error block.

I made the status 200 but it still says “0” in the label. Would I have to change the properties to get the nutriscore or food grade(A,B,C,D,E)?

How do you know that that’s wrong? If you get any value back from a JSON object then you parsed it correctly. :slight_smile: I just checked a barcode and its “positive_points” value is zero, for example.

If you want other property values then you have to figure out the “path” / hierarchy to those properties. You can re-watch my video if it’s helpful to understand that better.

The one thing I would recommend for you is that you ignore the “status” block. Just check if the green “error” block has a value and if not, start putting values into labels. Because the way you have it set up, it’s only going to check the “negative_points” property if the status is not 200.

You also need to figure out what kind of information you’re getting from the JSON data and how you’re going to display it. It looks like your intention was to display both the positive_points and negative_points in the same label at the same time. That’s not going to work because as soon as you assign a value to a label, it replaces the old value.

I created two different labels for the positive and negative points. But I’m not sure what other properties would be needed to get the food grade. This screenshot shows how to calculate and get the food grade, but I don’t know which property would incorporate this as the result.

I added some more blocks and values. Is this correct?

I have no idea, either. I don’t know anything about the API you’re using. It may explain that in the API documentation.

I would ask you… does it work?

Again, I’d recommend not using the “status” block at all. You’re creating a set of blocks that will never run because of how you are setting up your if/else statements.

Hi! Sorry for responding so late I was working on the project. I think that these blocks should work but when I test it no text shows up? Is there an error with the code?

You’re getting really side-tracked. I think you might need someone to walk you through this over Zoom/etc.

The green response block contains the wall of text that you get when you put the API url into a browser. So to then check to see if “response” = a number between 0 and 2 doesn’t make any sense. It would be like saying “does a dictionary equal four?”

You should only use the green response block at the very beginning to get the object from the API. After that, you should only be using “get property of object” blocks to get individual pieces of data from the JSON.

If you’re a student, do you have a teacher who is able to help you with this?

1 Like

No I do not have a teacher to help me with this.
So what would I have to check in the “if” block?

This isn’t something I can really explain over forum posts. I’ve tried several times above. There are concepts related to programming that you may have to learn such as how if/else statements work and how variables (such as the green blocks in Thunkable) store information.

I can suggest re-watching my tutorial video and probably watching some other videos related to programming with Thunkable.