Valid Json data - How to find out ? [ Solved #5]

I have an application where I am getting some 9 values over a serial link. I am parsing the serial data and converting to a json formatted string for posting to a server using the POST method. But before posting I would like to know if the resultant string from the function below is a valid json string ( it does not seem to be as the server reports NULL value posting ). Is the way that I have formed the json string OK ? I get the following as the string when debugging using the serial monitor :

{"machine_id":"ABCD_TR","date":"2019-07-16","time":"16:44:12","rig_power_status":"POWER ON","rig_operation_status":"POWER ON","rig_kwh_value":"ABCD-1234","rig_dut_model":"10","rig_number_tested":"1001","rig_number_passed":"951"}

And the function used to create the json string from data received via serial is as below :

void parseSerialData () {

  int index;

  if (newData == true)
  {
    //        Serial.print("Data string from PC test ");
    //        Serial.println(receivedChars);
   
    String receivedChars1 = receivedChars;

    index = receivedChars1.indexOf(',');

    jsonData = "{\"machine_id\":";
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"date\":";
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"time\":";
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"rig_power_status\":";                                   // On or Off
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"rig_operation_status\":";                               // Testing / Standby / Breakdown
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"rig_kwh_value\":";
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"rig_dut_model\":";
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"rig_number_tested\":";
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += ",\"rig_number_passed\":";
    receivedChars1 = receivedChars1.substring(index + 1);
    index = receivedChars1.indexOf(',');
    jsonData += "\"" + (String)receivedChars1.substring(0, index) + "\"";

    jsonData += "}";

    newData = false;
  }
}

Or is there a way to form the Json data with char variables without using the String type ?

Looks like valid JSON to me. Assuming that's what you're actually sending in your POST, it sounds you need to debug the issue on the receiving side. Are you sure your field names and data format are what it expects?

wildbill:
Looks like valid JSON to me. Assuming that's what you're actually sending in your POST, it sounds you need to debug the issue on the receiving side. Are you sure your field names and data format are what it expects?

Thanks.

Actually I have been grilling the guy who wrote the server side PHP … but his claim is this : The Post that he gets is a NULL one and that is why I am looking back at my code . The same server reports the data well if I post plain text messages.

Is there anyway to build the json string using char array rather than the String ( that every one "loves" so much !) ? sprintf() ??

You can use C string functions such as sprintf and strcpy to build your json string. You can also get a string from a String using c_str, which would be less effort just to run a test.

Best post the code that you're using.

wildbill:
You can use C string functions such as sprintf and strcpy to build your json string. You can also get a string from a String using c_str, which would be less effort just to run a test.

Best post the code that you're using.

Converting the jsonData which was in String data type to char array solved the problem. The code below shows that conversion. The jsonDataChar is declared as char array globally.. of course I can do it inside the function too. Anyway problem solved … :slight_smile: :slight_smile:

void postJsonData () {

  HTTPClient http;                     //Declare object of class HTTPClient

  digitalWrite(13, HIGH);

  http.begin("http://encorelectro.com/datacoll.php");                                          //Specify request destination
  http.addHeader("Content-Type", "application/json" );    //Specify content-type header

  jsonData.toCharArray(jsonDataChar, 250);

  int httpCode = http.POST(jsonDataChar);   //Send the data to save on server

  Serial.print( " POSTed to Server...");
  Serial.println( jsonDataChar);

  String payload = http.getString();             //Get the response payload
  Serial.println(httpCode);                           //Print HTTP return code

  digitalWrite(13, LOW);

  http.end();                                              //Close connection
}