Best tactic to decode JSON data..

For general Flowcode discussion that does not belong in the other sections.
MJU20
Posts: 302
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 88 times
Been thanked: 62 times

Flowcode v10 Best tactic to decode JSON data..

Post by MJU20 »

I have a GET request for openweather.org that returns the current weather in JSON format like this:

Code: Select all

{"coord":{"lon":14.4028,"lat":31.2602},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"base":"stations","main":{"temp":1.81,"feels_like":1.81,"temp_min":0.06,"temp_max":3.08,"pressure":1029,"humidity":94,"sea_level":1029,"grnd_level":1027},"visibility":10000,"wind":{"speed":0.89,"deg":68,"gust":1.34},"clouds":{"all":60},"dt":1732921533,"sys":{"type":2,"id":2012050,"country":"BE","sunrise":1732951431,"sunset":1732981133},"timezone":3600,"id":2798873,"name":"Ekeren","cod":200}
I've found the JSON decoder component, but don't know how to use it.

For now I use the NetworkComms SEND a

Code: Select all

 "GET http://api.openweathermap.org/data/2.5/weather?lat=41.26197&lon=14.402771&appid=900447etvtbgyjnki5514299984&units=metric\r\n\r\n"
and receive the response in a NetworkComms Receive component.

That puts the above value in a string.
But I want to retrieve certain values like "temp" out of this string and use it further in the flowchart.

Can I use the JSONdecoder to get the value for temp (and others) for this?
Or should I use another component to retrieve the wanted value?

Any help is welcome..

chipfryer27
Valued Contributor
Posts: 1367
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 330 times
Been thanked: 475 times

Re: Best tactic to decode JSON data..

Post by chipfryer27 »

Hi

Sorry to be brief. The JSON decoder will parse the reply for whatever "pair" you want.

The wiki has a good page and example

https://www.flowcode.co.uk/wiki/index.p ... r_(_Comms)

I have an example but no access to it just now.

Regards

MJU20
Posts: 302
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 88 times
Been thanked: 62 times

Re: Best tactic to decode JSON data..

Post by MJU20 »

Thanks Chipfryer27

I've used the example on the wiki but it returns strange results.

Let's say that the returned JSON code is:

Code: Select all

{"coord":{"lon":14.4038,"lat":35.2602},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"stations","main":{"temp":8.67,"feels_like":6.31,"temp_min":7.84,"temp_max":9.08,"pressure":1016,"humidity":94,"sea_level":1016,"grnd_level":1015},"visibility":10000,"wind":{"speed":4.12,"deg":200},"clouds":{"all":100},"dt":1733094005,"sys":{"type":2,"id":2012050,"country":"BE","sunrise":1733124395,"sunset":1733153859},"timezone":3600,"id":2798873,"name":"Dusseldorf","cod":200}
And search for "temp", this returns as index (for instance) 7.. (where I should think this would be 8, but maybe it starts from 0).
But when I return index 7 this gives me another value (let's say the value of "icon").

I found this by testing the component. It's confusing.
Now I got it working by looking for a value and return the indexnumber + 3..


(***), the above examples may be wrong because I wrote this as a draft but this disappearance :-(

BenR
Matrix Staff
Posts: 1909
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 494 times
Been thanked: 672 times

Re: Best tactic to decode JSON data..

Post by BenR »

Hello,

I can see what's going wrong, I updated the component so it could better deal with nested array data (counts , and { characters) but it looks like I didn't update the FindName function to match (was just counting , characters).

I've just pushed an update to the component that should solve the issue and should return the correct index of 10 for the temp field.

Let me know how you get on.

MJU20
Posts: 302
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 88 times
Been thanked: 62 times

Re: Best tactic to decode JSON data..

Post by MJU20 »

This is a great improvement.
The index is returned like it should and when retrieving the value for the index this is the right value too!
Thank you Ben!!

A small question about this macro still remains: when I retrieve the value for "temp" as a Float, it only shows the first digit, not the decimals..
Another thing to look into? When I retrieve it as a string it is shown completely.
BenR wrote:
Mon Dec 02, 2024 9:51 am
Hello,

I can see what's going wrong, I updated the component so it could better deal with nested array data (counts , and { characters) but it looks like I didn't update the FindName function to match (was just counting , characters).

I've just pushed an update to the component that should solve the issue and should return the correct index of 10 for the temp field.

Let me know how you get on.

BenR
Matrix Staff
Posts: 1909
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 494 times
Been thanked: 672 times

Re: Best tactic to decode JSON data..

Post by BenR »

Hello,

That's great glad it's working better for you now :D Thanks for letting us know.

Just checked the component source and ReadDataAsFloat seems to be working ok for me. What chip type are you using and I'll test again on there just to check it's not a specific CAL/toolchain issue.
Screenshot 2024-12-02 213912.jpg
Screenshot 2024-12-02 213912.jpg (69.04 KiB) Viewed 984 times

MJU20
Posts: 302
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 88 times
Been thanked: 62 times

Re: Best tactic to decode JSON data..

Post by MJU20 »

I use the ESP32 Wroom 32 (Node MCU module) together with an ILI9488 display

I never use the simulation mode because I (in the past) found out it sometimes gave different results like a real world device..

MJU20
Posts: 302
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 88 times
Been thanked: 62 times

Re: Best tactic to decode JSON data..

Post by MJU20 »

When I read the returned data (string), and pass it trough the StringToFloat$(string) function in a calculation block, it only returns the part before the decimal divider.

In this case the "9.48" returns only the 9.. I need the .48 too :-)

Is this specific an issue with the ESP32?

chipfryer27
Valued Contributor
Posts: 1367
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 330 times
Been thanked: 475 times

Re: Best tactic to decode JSON data..

Post by chipfryer27 »

Hi

Sorry to ask a stupid question, but is the variable you are using to capture the float value in your StringToFloat, actually assignad as a float?

e.g. x = StringToFloat$("9.987654321")

If x is a byte or integer then you will not get any decimal places in your answer.

When I create variables I usually assign them an initial value, normally 0 or in the case of a float 0.0. Helps me check I have assigned things correctly and is easily overlooked.

Regards

MJU20
Posts: 302
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 88 times
Been thanked: 62 times

Re: Best tactic to decode JSON data..

Post by MJU20 »

Thank you chipfryer27, this is again a good suggestion!

But this variable is as float as something floating like very small rocks ( https://www.youtube.com/watch?v=rf71YotfykQ ).

I tried your suggestion by setting an initial value and this compiles.
Then I tried to add a calculation block with a random value "9.445", but this is only show as a value without the decimals.

So 9.445 is shown by the ILI9488 as "9", the ILI9488 component shows the number not as a float...

Post Reply