JSON to static data

I have a problem with JSON. I have a pointer into the JSON data, but in Void Loop the data is overwritten and my program can't execute with the JSON data. Only sometimes the JSON data is damaged and overwritten. I have also tried with StaticJsonDocument but it didn't compile. Don't what I did wrong. I am not very good at programming Arduino.

Can you help to move JSON data to a simple array? Please just edit the code below directly and upload here.

The JSON data just contains H_RESPITSEK=x; H_RESPITSEK=y; H_RESPITSEK=z; and so on. The length of the JSON is variates and therefore I need x,y,z in a simple array for the rest of the program.

A part of the code:


const char* RESPITSEKARR[1000];    // <= this must contain static data which is not overwritten.

void setup(){
    if(H_RESPITSTART=="J"){
      String serverPathData3 = serverName + "?klubid=" + KLUBID + "&hornres=J";     // set server and request data
      Serial.print("  ");   
      Serial.println(serverPathData3);                                               // print server path
      http.begin(client, serverPathData3.c_str());                                   // begin request
      httpResponseCode = http.GET();                                                // get data
      if (httpResponseCode > 0) {                                                   // if http runs OK
        Serial.print("  JSON HTTP Response code: ");                                // print http code
        Serial.println(httpResponseCode);
        String payload3 = http.getString();                                         // read http response
        Serial.print("  ");
        Serial.println(payload3);                                                   // print http response

        //https://arduinojson.org/ --> Assistent menu, to generate json code without nested elements
        //StaticJsonDocument<512> doc;                                              // define static JSON example
        DynamicJsonDocument doc(5000);                                              // define dynamic JSON with beginning size
        DeserializationError error = deserializeJson(doc, payload3);                // parsen JSON data to doc
        if (error) {                                                                // if parse error
          Serial.print("  deserializeJson() failed: ");                             // print error
          Serial.println(error.c_str());
          return;
        }
        JsonArray nested = doc.as<JsonArray>();                                     // JSON array is nested (two dimensionel)

    
        //NUMBER OF LINES IN JSON
        int arrayLength3 = doc.size();                                               // size of doc equals number of array lines
        respitlines= doc.size();                                                    // put array lines to var
        Serial.print("  JSON lines = ");                                            // print number of array lines
        Serial.println(arrayLength3);

        int i = 0;
        for (JsonObject item : nested) {
          // * = is a pointer into the JSON file. JSON will be overwritten later
          // DATA will be overwritten since item is a pointer
          DATA[i][6] = item["H_RESPITSEK"];
          RESPITSEKARR[i]=DATA[i][6];
          Serial.print("  RESPITSEK ARR = ");
          Serial.println(RESPITSEKARR[i]);
          i++;
        }

      } // ends if for horn respit data 
      else {
        Serial.print("  Error code: ");             // else print http error code if wifi not connected
        Serial.println(httpResponseCode);
      }
      http.end();                                   // close http connection
    } //respit read in finished
}

void loop(){
   some code here
}

can you give us an example of the exact JSON?

It looks like you are just copying pointers to your strings. If you want a copy, you have to copy the string itself using strcpy(). This will prevent the data from being overwritten. It also means that your array RESPITSEKARR can not just be an array of char *, but actual memory to hold the result of the copy.

What board are you using? You are using up a pretty big chuck of memory for your json object.

Can you please write the needed code to define the RESPITSEKARR variable?
Can you please write the needed code for the copy function?
Or simply edit edit my code and return on this page?

Does RESPITSEKARR[1000] mean 1000 bytes or 1000 numbers? I typically need space for up to 50 numbers each having a length of up to 4 characters (numbers).

I have enough memory, that is not the problem. The program works in 8 out of 10 times. So I think that the problem is, that the memory now and then is overwritten by other code.

Here you can see the JSON result and the result for RESPITSEKARR. In now 9 out of 10 times RESPITSEKARR contains numbers but the now and the RESPITSEKARR are all 0 after some time.

JSON HTTP Response code: 200
[{"H_RESPITSEK":"300"},{"H_RESPITSEK":"594"},{"H_RESPITSEK":"600"},{"H_RESPITSEK":"622"},{"H_RESPITSEK":"650"},{"H_RESPITSEK":"814"},{"H_RESPITSEK":"868"},{"H_RESPITSEK":"904"},{"H_RESPITSEK":"918"},{"H_RESPITSEK":"1182"}]
JSON lines = 10
RESPITSEK ARR = 300
RESPITSEK ARR = 594
RESPITSEK ARR = 600
RESPITSEK ARR = 622
RESPITSEK ARR = 650
RESPITSEK ARR = 814
RESPITSEK ARR = 868
RESPITSEK ARR = 904
RESPITSEK ARR = 918
RESPITSEK ARR = 1182

no. That is not how this forum works. If you want to hire somebody to write your code for you, then post over on the collaboration forum and pay.

In this forum, people help people who are trying.

that's 1000 pointers to characters. Probably not what you need.

--
consider this example (ArduinoJSON 7)

#include <ArduinoJson.h>
String payload3 = "[{\"H_RESPITSEK\":\"300\"},{\"H_RESPITSEK\":\"594\"},{\"H_RESPITSEK\":\"600\"},{\"H_RESPITSEK\":\"622\"},{\"H_RESPITSEK\":\"650\"},{\"H_RESPITSEK\":\"814\"},{\"H_RESPITSEK\":\"868\"},{\"H_RESPITSEK\":\"904\"},{\"H_RESPITSEK\":\"918\"},{\"H_RESPITSEK\":\"1182\"}]";

const size_t maxEntries = 20;
int hRespitsekArray[maxEntries];
size_t hRespitseCount = 0;

void setup() {
  Serial.begin(115200);

  JsonDocument doc;

  DeserializationError error = deserializeJson(doc, payload3);

  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    return;
  }

  if (doc.size() > maxEntries) {
    Serial.printf("Too many entries (%zu), stopping at %zu\n", doc.size(), maxEntries );
  }

  hRespitseCount = 0;
  for (JsonObject item : doc.as<JsonArray>()) {
    const char* str = item["H_RESPITSEK"];
    if (sscanf(str, "%d", hRespitsekArray + hRespitseCount) == 1) {
      Serial.printf("#%zu\t%d\n", hRespitseCount+1, hRespitsekArray[hRespitseCount]);
      hRespitseCount++;
      if (hRespitseCount >= maxEntries) break;
    } else {
      Serial.printf("Error scaning an integer from [%s]\n", str);
      break;
    }
  }
  // here hRespitseCount contains the number of entries in the array
}

void loop() {}

I'm filling the hRespitsekArray array with integers extracted from the JSON and I maintain a variable hRespitseCount that will tell you how many entries were read.

You also have a few tests to check for bounds and properly parsing the integer.

The Serial monitor should show

#1	300
#2	594
#3	600
#4	622
#5	650
#6	814
#7	868
#8	904
#9	918
#10	1182

Thank you very much. This answer really solved my problems for a long time. I think the program is working correct now. I am testing and testing.

Have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.