Page 1 of 1

Help with JSON and arrays - Resolved.

Posted: Fri Apr 09, 2021 4:07 pm
by AndyGadget
I'm trying to extract JSON data from ThingSpeak into an array using the (stripped down) code below and am seeing the first element as the correct value but elements 2 and 3 are read as zero. Is my JSON format request correct for filling this array?

Code: [Local Link Removed for Guests]

for lcnt = 1 to 3
  IntTemp(lcnt) = val(json$(NewData$,"feeds[" + str$(lcnt) + "].field4"))
  wlog "Query string : feeds[" + str$(lcnt) + "].field4" + "     Response " +    str$(IntTemp(lcnt) )
next lcnt
Resulting in the wlog output :
Query string : feeds[1].field4 Response 12.6
Query string : feeds[2].field4 Response 0
Query string : feeds[3].field4 Response 0

UPDATE - SORTED!!!
It works if I move the array reference to the end :
json$(NewData$,"feeds.field4[1]")
json$(NewData$,"feeds.field4[2]")
json$(NewData$,"feeds.field4[3]")

Which may be at odds with correct JSON structure and the example in the help file?


This is the JSON data received from ThingSpeak after running through a formatter.

Code: [Local Link Removed for Guests]

{
   "channel":{
      "id":1283507,
      "name":"Greenhouse Monitor",
      "description":"Monitoring internal and external greenhouse environment with ESP32 and 2 x BME280.\r\nESP32 is running Annex RDS. ",
      "latitude":"0.0",
      "longitude":"0.0",
      "field1":"Battery",
      "field2":"Wifi RSSI",
      "field3":"Atm. Pressure",
      "field4":"Internal Temp.",
      "field5":"Internal Hum.",
      "field6":"External Temp.",
      "field7":"External Hum.",
      "created_at":"2021-01-15T22:52:34Z",
      "updated_at":"2021-03-14T18:18:06Z",
      "last_entry_id":7654
   },
   "feeds":[
      {
         "created_at":"2021-04-09T15:29:20Z",
         "entry_id":7652,
         "field1":"3.7",
         "field2":"45",
         "field3":"1014",
         "field4":"12.6",
         "field5":"82",
         "field6":"8.4",
         "field7":"84"
      },
      {
         "created_at":"2021-04-09T15:39:18Z",
         "entry_id":7653,
         "field1":"3.7",
         "field2":"45",
         "field3":"1014",
         "field4":"12.6",
         "field5":"82",
         "field6":"8.5",
         "field7":"84"
      },
      {
         "created_at":"2021-04-09T15:49:17Z",
         "entry_id":7654,
         "field1":"3.7",
         "field2":"45",
         "field3":"1014",
         "field4":"12.5",
         "field5":"82",
         "field6":"8.4",
         "field7":"83"
      }
   ]
}

Re: Help with JSON and arrays - Resolved.

Posted: Sat Apr 10, 2021 7:25 am
by cicciocb
Yes, the JSON parser included in Annex is not a "true" json parser but more a "search engine".
I did it many time ago mainly with the objective to consume as less RAM as possible.

I fact, it works looking for the occurrence of each elements defined in the json$ command in sequence.
Because "feeds" is defined only once in the data, 'feeds[1]' will works but "feeds[2]" and so on will not work.

In your case, you found that it works with json$(NewData$,"feeds.field4[1]")

This is because the JSON$ function looks before for the first occurrence of the element "feed" and then it looks for the first occurrence of the element "field4".
Using json$(NewData$,"feeds.field4[2]") looks before for the first occurrence of the element "feed" and then it looks for the second occurrence of the element "field4".

This can considered as a different way to specify the JSON search pattern.

I already tried to implement a "real" JSON parser but each time I found that the impact in terms of RAM and memory space was not effective considering the resources of the chips (in particular for the ESP8266).

Re: Help with JSON and arrays - Resolved.

Posted: Sat Apr 10, 2021 8:29 am
by AndyGadget
That makes perfect sense - Thanks for the explanation, Cicciocb.

Your way may not be technically exact, but it does the job and I find it more logical than the 'correct' way.
You may want to update the manual example to mirror the way you've implemented it.