JSON parse issues

I'm parsing JSON in my Arduino sketch and having issues. I think it has to do with escaping characters but not sure. I'm calling the web service from Adafruit QT PY using Python without any issues.

String wsResponse = clientHTTP.responseBody();
Serial.print("response: ");
Serial.println(wsResponse);

//prints   {\"ResponseCode\":1,\"ResponseMessage\":\"Success\",\"LogID\":13106}

StaticJsonDocument<200> jsonBuffer;         //40 is probably enough

char json[wsResponse.length()+1];                
wsResponse.toCharArray(json, wsResponse.length()+1);
              
DeserializationError error = deserializeJson(jsonBuffer, json);
int wsResponseCode = jsonBuffer["ResponseCode"];

if test with the following text it works as expected
String wsResponse = "{\"ResponseCode\":1,\"ResponseMessage\":\"Success\",\"LogID\":13106}";
There are backslashes in the text, not sure why will display

I don't think it's an escaping characters issue, anyway does "error.code" returned something Like "DeserializationError::Ok" or what else (see Deserialization error)?

Try this:

...
  Serial.print("json=\"); Serial.print(json); Serial.println("\"");
  DeserializationError error = deserializeJson(jsonBuffer, json);
  if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
   }
  Serial.print("ResponseCode: ");Serial.println(jsonBuffer["ResponseCode"]);
  int wsResponseCode = jsonBuffer["ResponseCode"];
  Serial.print("wsResponseCode=");Serial.println(wsResponseCode);

and show us the results.

Serial.println("json=""); Serial.print(json); Serial.println(""");
json="{"ResponseCode":1,"ResponseMessage":"Success","LogID":13106}"

Serial.println(error.code());
0

Ok, so the Json string looks ok, and no error on deserialize.
But you didn't use my code (and missed "wsResponseCode" value).
Please change the code to include my modifications and paste here the whole and only serial monitor output.

I think you are right, that the JSON document should not have backslashes in it. I think something is going wrong on the sending side.

Note: There is no need to copy your String into a character array to deserialize:

// String input;

StaticJsonDocument<96> doc;

DeserializationError error = deserializeJson(doc, input);

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

int ResponseCode = doc["ResponseCode"]; // 1
const char* ResponseMessage = doc["ResponseMessage"]; // "Success"
int LogID = doc["LogID"]; // 13106

Where do you see backslashes? Based on the OP output there are no backslashes at all:

json="{"ResponseCode":1,"ResponseMessage":"Success","LogID":13106}"

I was going by this comment:

Serial.println(wsResponse);

//prints   {\"ResponseCode\":1,\"ResponseMessage\":\"Success\",\"LogID\":13106}

John and Doc, thank you for your assistance.

Doc, I reread your post but I cannot get your code to compile. I receive the following error on line, Serial.println(jsonBuffer["ResponseCode"]);

GetHelp:599:60: error: call of overloaded 'println(ArduinoJson6194_F1::enable_if<true, ArduinoJson6194_F1::MemberProxy<ArduinoJson6194_F1::JsonDocument&, const char*> >::type)' is ambiguous
Serial.println(jsonBuffer["ResponseCode"]);

I thought I found the issue but still no luck. I was serializing my content to JSON twice so that is why I was seeing the backslashes. However I updated the code to only serialize once and still have the issue. I called my service via Postman and see the Content-Type is set to JSON and the body looks like JSON now. Not sure what to do

Thanks for your assistance.

You're right, my fault. The "jsonBuffer" variable is a "StaticJsonDocument" type, not directly handled by Serial.print() so you should try explicitly cast it. I don't have now access to my development PC at home to give it a try, but this could/should work:

  String wsResponse = clientHTTP.responseBody();
  Serial.print("response: "); Serial.println(wsResponse);
  char json[wsResponse.length()+1];                
  wsResponse.toCharArray(json, wsResponse.length()+1);
  StaticJsonDocument<200> jsonBuffer;         //40 is probably enough
  DeserializationError error = deserializeJson(jsonBuffer, json);
  if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.f_str());
      return;
   }
  Serial.print("jsonBuffer[\"ResponseCode\"] = \"");
  const char* ResponseCode = jsonBuffer["ResponseCode"];
  Serial.print(ResponseCode);
  Serial.println("\"");

  int wsResponseCode = jsonBuffer["ResponseCode"];
  Serial.print("wsResponseCode=");Serial.println(wsResponseCode);

The Serial.print(ResponseCode); is just to get what the deserialized json buffer contains, then converted to an int: if it's empty, the wsResponseCode int value will be obviously zero, and something went wrong with the deserializeJson() function (but if "error" is not returned, I really can't understand why).
Please compile and run this code and post here the serial output.

PS: if you need/want to directly use String variable, the ArduinoJson documentation says you can do it, but should define this symbol first:

#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#include <ArduinoJson.h>

First thank you, turns out I had a sequence of errors that are now resolved. First I double serialized my JSON string which led to the backslashes I mentioned in my original post. Then when I fixed my web service to only serialize the JSON once it started returning in camel case which was causing the last error we were discussing. So all is good now, except I cannot figure out how not to use a String to receive clientHTTP.responseBody();

Try something like this to get on a char array the first row of received data (my Arduino develop PC is still out of order, I suppose the CPU has gone :sob:, so I can't make sure it works..):

const int BUFFERSIZE = 80; // Set the highest size here + 1
char buf[BUFFERSIZE]; 
int ptr = 0;
char c;
while (clientHTTP.available() && ptr < BUFFERSIZE) {
  c = clientHTTP.read();
  if (c == '\n') // End of line
    break;
  if (c != '\r') // Ignore CRs
  {
    buf[ptr++] = c;
    buf[ptr] = '\0';
  }
}
// Here you have "buf" filled with the first response line (I hope it's the json data you need)

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