Page 1 of 1

JSON Decoder ESP32

Posted: Thu Oct 03, 2024 10:42 am
by Xbiotec
hi,
I'm trying to decode JSON string using JSON DECODER.
1- when I'm trying to decode this string

Code: Select all

{"power":500,"frequency":20000,"total_energy":"0.001000","duration":60}
First I checked how many data in the string and result is : 4

data 0 power 500
data 1 frequency 20000
data 2 total_energy 0
data 3 duration 60
No problem, I can get all the name and data

2- when I'm trying to decode this string

Code: Select all

{
	"usb_ports":	[{
			"USB_1":	{
				"power":	500,
				"duration":	5,
				"frequency":	20000,
				"total_energy":	"0.000",
				"ischeck":	true
			}
		}, {
			"USB_2":	{
				"power":	0,
				"duration":	0,
				"frequency":	0,
				"total_energy":	"0.000",
				"ischeck":	true
			}
		}, {
			"USB_3":	{
				"power":	0,
				"duration":	0,
				"frequency":	0,
				"total_energy":	"0.000",
				"ischeck":	true
			}
		}, {
			"USB_4":	{
				"power":	0,
				"duration":	0,
				"frequency":	0,
				"total_energy":	"0.000",
				"ischeck":	true
			}
		}]
}
First I checked how many data in the string and result is : 25
data 0 usb_ports 1
data 1 duration 5
data 2 frequency 20000
data 3 total_energy 0
data 4 ischeck 0
data 5 USB_2 0
data 6 duration 0
data 7 frequency 0
data 8 total_energy 0
data 9 ischeck 0
data 10 USB_3 0
data 11 duration 0
data 12 frequency 0
data 13 total_energy 0
data 14 ischeck 0
data 15 USB_4 0
data 16 duration 0
data 17 frequency 0
data 18 total_energy 0
data 19 ischeck 0
data 20 0
data 21 0
data 22 0
data 23 0
data 24 0

Problem, USB_1 and the 4 power data are missing
May because the decoder is not able to decode an array ?
or other ?
when I check if "power" name is present, the result 65535


Thx for your prompt reply

Re: JSON Decoder ESP32

Posted: Thu Oct 03, 2024 11:06 am
by Xbiotec
I forgot to tell you but I checked the Json with Json Parser Online and the frame is good.
So I think it's a decoder component problem
Json parser online.jpg
Json parser online.jpg (65.93 KiB) Viewed 14669 times

Re: JSON Decoder ESP32

Posted: Thu Oct 03, 2024 11:49 am
by Xbiotec
same problem with this JSON frame

Code: Select all

{"modes":{"fastmode":{"completed":true}}}
DataCount 3
data 0 modes 0
data 1 0
data 2 0

only "modes" can be found

Re: JSON Decoder ESP32

Posted: Thu Oct 03, 2024 2:04 pm
by BenR
Hello,

Currently the JSON encoder/decoder can only work with flat style JSON data as in your first example.

Supporting arrays would indeed be nice so I'll see if it's something we can add.

Re: JSON Decoder ESP32

Posted: Thu Oct 03, 2024 2:36 pm
by Xbiotec
yes please Ben because on my project, I don't have only flat style JSON and it's a big problem for me .

Re: JSON Decoder ESP32

Posted: Thu Oct 10, 2024 10:55 am
by BenR
Hello,

Right I've made a change to the decoder component and it should hopefully now work a lot better when working with nested data.

For your example

Code: Select all

{"modes":{"fastmode":{"completed":true}}}
I get a count of 3 values detected but now the names and values look like this.

Code: Select all

"modes" - "fastmode"
"fastmode" - "completed"
"completed" - "true"
For your first example it should also behaive better in terms of storage space and remove all unnessisary formatting like spaces and new lines outside of quoted strings.

It's now available via the library updates. Let me know how you get on.

Re: JSON Decoder ESP32

Posted: Tue Oct 15, 2024 6:40 am
by Xbiotec
Thx Ben, I will test and let you know.