Printing JSON data only from when the character { is detected

Hi,
I would like to print JSON data only when character "{" is detected until "}" to avoid printing HTTP headers.

String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  Serial.print("Command Sent: "); Serial.println(command);
  A9G.println(command);

  unsigned long time = millis();
  while (A9G.available() == 0) {}

  while (A9G.available())
  {
    char c = A9G.read();
    response += c;
//    Serial.print(c);
    delay(10);
  }

  if (debug)
  {
    Serial.println();
    Serial.print("A9G Response: ");
    Serial.println(response);
  }

  if (response.indexOf('{') > 0) {
    int l = response.indexOf('{');
    String res = response.substring(l);
    //    Serial.println(res);
    response = res;
  }
  return response;
}

Where are you stuck ?

Please do not post pictures of Serial monitor output. Select what you want to post with the mouse, press Ctrl+C to copy it to the clipboard and paste it here in code tags

Please post an example of the String that you are trying to parse. Does it have to be a String or could it be a C style string ?

I want to get below response only, not to print the http headers.

Ignore printing below:

5:24:45.071 -> date: Mon, 21 Feb 2022 11:24:35 GMT
15:24:45.071 -> content-type: text/html; charset=UTF-8
15:24:45.071 -> transfer-encoding: chunked
15:24:45.071 -> vary: Accept-Encoding
15:24:45.071 -> server: Apache
15:24:45.071 -> x-powered-by: PHP/7.4.28
15:24:45.071 -> cache-control: no-cache, private
15:24:45.071 -> access-control-allow-origin: *
15:24:45.071 -> access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS
15:24:45.071 -> x-ratelimit-limit: 60
15:24:45.071 -> x-ratelimit-remaining: 59
15:24:45.071 -> x-provided-by: StackCDN
15:24:45.071 -> vary: Accept-Encoding
15:24:45.071 -> x-origin-cache-status: MISS
15:24:45.071 -> x-service-level: standard
15:24:45.071 -> x-backend-server: web6.hosting.stackcp.net
15:24:45.118 -> x-cdn-cache-status: MISS
15:24:45.118 -> x-via: LHR4
15:24:45.118 -> Content-Length: 262

Need to Print below JSON data only:

HTTP/1.1  200  OK
{"isArm":"1","wifi":"0","wifi_password":"","wifi_connssid":"0","wifi_disconnect":0,"wifi_disc_name":"","engine":1,"phoneNumber":"542351685","number1":"","number2":"","number3":"","number4":"","number5":"","gyro":"0","gps":"1","powerSupply":"1","plateNo":"32568"}

You may need some HTTP library that handles the response and "pretties" it up into header, body, etc. And then some JSON library that takes the response body text and parses it.

Also looking for just the { character may not work well because JSON can contain nested {...} elements.

If its just one {...} block, and you have to do it all yourself (no library available) then you may be able to use regular expressions to extract that from the full response body and then further parse it with the commas and colons to extract name/value pairs.

  1. Extract the part between { and }
  2. Split using ',' into an array
  3. For each array element split using ':' to get another array of 2 elements - 1st element is the name, 2nd is the value

I have already got the name and value , only need to print JSON response without the http headers. So, if you have any idea let me know how to do it.

The example messages that you posted do not include a JSON message. As I asked before, does the message need to be stored in a String or could it be stored in a C style string ?

In fact, does the complete message need to be stored at all ? Could you start storing the JSON section only on receipt of a { and stop when you receive a }

Please post a full sketch that illustrates what you are doing and how you receive the message


#include <WiFi.h>
#include <HTTPClient.h>
//#include <Arduino_JSON.h>
char ssid[] = "Ayaz 2.46GHz";         // your SSID
char pass[] = "Mohammad@786";     // your SSID Password

#include <HardwareSerial.h>
#define DEBUG true
#define BAUD_RATE 115200
#include<stdio.h>
#include <ArduinoJson.h>
#define A9G_SIZE_RX  1024    // used in A9G.setRxBufferSize()
#define data_BUFFER_LENGTH   1024
/***********************************/
#define A9G_PON     13  //ESP12 GPIO16 A9/A9G POWON
#define A9G_POFF    12  //ESP12 GPIO15 A9/A9G POWOFF
#define A9G_WAKE    27  //ESP12 GPIO13 A9/A9G WAKE
#define A9G_LOWP    14  //ESP12 GPIO2 A9/A9G ENTER LOW POWER MODULE

int A9GPOWERON();

String response = "";
HardwareSerial A9G(1);
int WiFi_Count=10;
void setup() {

  Serial.begin(115200); /* Define baud rate for A9G communication */
  A9G.begin(9600, SERIAL_8N1, 35, 32);    // RX and TX of ES32
  A9G.setRxBufferSize(A9G_SIZE_RX);

  WiFi.begin(ssid, pass);
  Serial.println("");
 Serial.print("Connecting");
  // Wait for connection
  
  while (WiFi.status() != WL_CONNECTED) {
    if (WiFi_Count>0) {
    delay(500);
    Serial.print(".");
    WiFi_Count=WiFi_Count-1;
  }
  else goto label;
  }
//If connection successful show IP address in serial monitor
  label:
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("WIFI Local IP:"); 
  Serial.print(WiFi.localIP());           //IP address assigned to your ESP
  
  Serial.println("\nA9G Test Started...");

  for (char ch = ' '; ch <= 'z'; ch++) {
    A9G.write(ch);
  }
  A9G.println("");
  delay(500);
  //    sendData("AT+RST=1", 1000, DEBUG); //Software reset
  //    delay(500);

  /************************************/
  pinMode(A9G_PON, OUTPUT);//LOW LEVEL ACTIVE
  pinMode(A9G_POFF, OUTPUT);//HIGH LEVEL ACTIVE
  pinMode(A9G_LOWP, OUTPUT);//LOW LEVEL ACTIVE

  digitalWrite(A9G_PON, HIGH); // Keep A9G OFF(HIGH) initially
  digitalWrite(A9G_POFF, LOW);
  digitalWrite(A9G_LOWP, HIGH);

  Serial.println("Turning ON A9G...");
  Serial.println();

  if (A9GPOWERON() == 1)
  {
    Serial.println("A9G POWER ON.");
    Serial.println();
  }

  sendData("ATE0", 1000, DEBUG);
  sendData("AT+CGATT=0", 1000, DEBUG);
  sendData("AT+CGSN", 3000, DEBUG);  //IMEI No.
  sendData("AT+CCLK?", 500, DEBUG);
  sendData("AT+CREG?", 3000, DEBUG);
  sendData("AT+CSQ", 1000, DEBUG);
  sendData("AT+CMGF=1", 1000, DEBUG);
  sendData("AT+CPMS=\"SM\",\"SM\",\"SM\"", 1000, DEBUG);
  //  sendData("AT+CMGDA=\"DEL ALL\"", 1000, DEBUG); //Delete All Messages
  sendData("AT+GPS=1", 1000, DEBUG);
  sendData("AT+GPNT=1", 500, DEBUG); //Set the status of GPS light
  sendData("AT+LOCATION=2", 500, DEBUG);  //1:Get base address, 2:get gps address

  sendData("AT+CGATT=1", 3000, DEBUG);  //Attach network, this command is necessary if the Internet is needed.
  sendData("AT+CGDCONT=1,\"IP\",\"cit-moi\"", 3000, DEBUG);  //Set PDP parameter
  sendData("AT+CGACT=1,1", 3000, DEBUG);  //Activate PDP, can access to Internet when activated properly
  sendData("AT+CIFSR", 1000, DEBUG); //Get IP address



}

void loop() {
//  while (A9G.available() > 0) {
//    Serial.write(A9G.read());
//    //    yield();
//  }
//  while (Serial.available() > 0) {
//    A9G.write(Serial.read());
//    //    yield();
//  }

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
  HTTPWiFi_Data();
}

else
  HTTPGPRS_Data();
}



void HTTPGPRS_Data() {

  //****For Sending Data to MySQL****
  //  String cmdString = "AT+HTTPGET=\"https://apk.vehtechs.com/api/imei-update-info?imei=123456789012345&lat=11.11&lng=22.22&speed=100&wifi_ssid1=ABC5GHz&wifi_ssid2=ABC5GHz&wifi_ssid3=ABC5GHz&wifi_ssid4=ABC5GHz&wifi_connected=1&gyro=1&battery=1&fuel=100&door1=1&door2=1&door3=0&door4=1\"";

  //****For Receivng Data from MySQL****
  String cmdString = "AT+HTTPGET=\"http://apk.vehtechs.com/api/imei?imei=123456789456789\"";
  String input = sendData(cmdString, 500, DEBUG);

  StaticJsonDocument<512> doc;

  DeserializationError error = deserializeJson(doc, input);

  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    //return;
  }
  else {
    const char* isArm = doc["isArm"]; // "1"
    const char* wifi = doc["wifi"]; // "0"
    const char* wifi_password = doc["wifi_password"]; // nullptr
    const char* wifi_connssid = doc["wifi_connssid"]; // "0"
    int wifi_disconnect = doc["wifi_disconnect"]; // 0
    const char* wifi_disc_name = doc["wifi_disc_name"]; // nullptr
    int engine = doc["engine"]; // 1
    const char* phoneNumber = doc["phoneNumber"]; // "542351685"
    const char* number1 = doc["number1"]; // nullptr
    const char* number2 = doc["number2"]; // nullptr
    const char* number3 = doc["number3"]; // nullptr
    const char* number4 = doc["number4"]; // nullptr
    const char* number5 = doc["number5"]; // nullptr
    const char* gyro = doc["gyro"]; // "0"
    const char* gps = doc["gps"]; // "1"
    const char* powerSupply = doc["powerSupply"]; // "1"
    const char* plateNo = doc["plateNo"]; // "32568"

    Serial.print("isArm= ");
    Serial.println(isArm);
    Serial.print("wifi= ");
    Serial.println(wifi);
    Serial.print("wifi_password= ");
    Serial.println(wifi_password);
    Serial.print("wifi_connssid= ");
    Serial.println(wifi_connssid);
    Serial.print("wifi_disconnect= ");
    Serial.println(wifi_disconnect);
    Serial.print("wifi_disc_name= ");
    Serial.println(wifi_disc_name);
    Serial.print("engine= ");
    Serial.println(engine);
    Serial.print("phoneNumber= ");
    Serial.println(phoneNumber);
    Serial.print("number1= ");
    Serial.println(number1);
    Serial.print("number2= ");
    Serial.println(number2);
    Serial.print("number3= ");
    Serial.println(number3);
    Serial.print("number4= ");
    Serial.println(number4);
    Serial.print("number5= ");
    Serial.println(number5);
    Serial.print("gyro= ");
    Serial.println(gyro);
    Serial.print("gps= ");
    Serial.println(gps);
    Serial.print("powerSupply= ");
    Serial.println(powerSupply);
    Serial.print("plateNo= ");
    Serial.println(plateNo);
    Serial.println();
    Serial.println();
    Serial.println();
  }
  delay(500);
}



String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  Serial.print("Command Sent: "); Serial.println(command);
  A9G.println(command);

  unsigned long time = millis();
  while (A9G.available() == 0) {}

  while (A9G.available())
  {
    char c = A9G.read();
    response += c;
//    Serial.print(c);
    delay(10);
  }

  if (debug)
  {
    Serial.println();
    Serial.print("A9G Response: ");
    Serial.println(response);
  }

  if (response.indexOf('{') > 0) {
    int l = response.indexOf('{');
    String res = response.substring(l);
//    Serial.println(res);
    response = res;
  }
  return response;
}


int A9GPOWERON()                 //Send Command to A9G to Turn ON
{
  digitalWrite(A9G_PON, LOW);
  delay(3000);
  digitalWrite(A9G_PON, HIGH);
  delay(5000);
  String msg = String("");
label:
  msg = sendData("AT", 2000, DEBUG);
  if ( msg.indexOf("OK") >= 0 ) {
    Serial.println("GET OK");
    Serial.println();
    return 1;
  }
  else {
    Serial.println("NOT GET OK");
    Serial.println();
    goto label;     //If NOT GET OK GOTO label
    return 0;
  }
}


void HTTPWiFi_Data() {

    HTTPClient http;  //Declare an object of class HTTPClient

    String ReceiveData = "http://apk.vehtechs.com/api/imei?imei=123456789456789";

    Serial.println(ReceiveData);
    Serial.println();

    http.begin(ReceiveData);  //Specify request destination
    int httpCode = http.GET();
    delay(500);
    httpCode = http.GET();  //Send the request

    if (httpCode > 0) { //Check the returning code

      Serial.print("HTTP Response code: ");
      Serial.println(httpCode);
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
      Serial.println();
      Serial.println();

      StaticJsonDocument<512> doc;

  DeserializationError error = deserializeJson(doc, payload);

  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    //return;
  }
  else {
    const char* isArm = doc["isArm"]; // "1"
    const char* wifi = doc["wifi"]; // "0"
    const char* wifi_password = doc["wifi_password"]; // nullptr
    const char* wifi_connssid = doc["wifi_connssid"]; // "0"
    int wifi_disconnect = doc["wifi_disconnect"]; // 0
    const char* wifi_disc_name = doc["wifi_disc_name"]; // nullptr
    int engine = doc["engine"]; // 1
    const char* phoneNumber = doc["phoneNumber"]; // "542351685"
    const char* number1 = doc["number1"]; // nullptr
    const char* number2 = doc["number2"]; // nullptr
    const char* number3 = doc["number3"]; // nullptr
    const char* number4 = doc["number4"]; // nullptr
    const char* number5 = doc["number5"]; // nullptr
    const char* gyro = doc["gyro"]; // "0"
    const char* gps = doc["gps"]; // "1"
    const char* powerSupply = doc["powerSupply"]; // "1"
    const char* plateNo = doc["plateNo"]; // "32568"

    Serial.print("isArm= ");
    Serial.println(isArm);
    Serial.print("wifi= ");
    Serial.println(wifi);
    Serial.print("wifi_password= ");
    Serial.println(wifi_password);
    Serial.print("wifi_connssid= ");
    Serial.println(wifi_connssid);
    Serial.print("wifi_disconnect= ");
    Serial.println(wifi_disconnect);
    Serial.print("wifi_disc_name= ");
    Serial.println(wifi_disc_name);
    Serial.print("engine= ");
    Serial.println(engine);
    Serial.print("phoneNumber= ");
    Serial.println(phoneNumber);
    Serial.print("number1= ");
    Serial.println(number1);
    Serial.print("number2= ");
    Serial.println(number2);
    Serial.print("number3= ");
    Serial.println(number3);
    Serial.print("number4= ");
    Serial.println(number4);
    Serial.print("number5= ");
    Serial.println(number5);
    Serial.print("gyro= ");
    Serial.println(gyro);
    Serial.print("gps= ");
    Serial.println(gps);
    Serial.print("powerSupply= ");
    Serial.println(powerSupply);
    Serial.print("plateNo= ");
    Serial.println(plateNo);
    Serial.println();
    Serial.println();
    Serial.println();
    }
    http.end();   //Close connection
  }
  delay(500);    //Send a request every 1 second 
}

There is a blank line between "Content-Length: 262" and your JSON. That blank line marks the end of the headers. Look for the blank line.

How? Please share a sample code

I think something like this should work.

  // Skip headers
  boolean lineIsEmpty = false;
  while (A9G.available())
  {
    char c = A9G.read();

    if (c == '\r')
      continue; // Ignore Return characters

    if (c == '\n')  // Newline/Linefeed character
    {
      if (lineIsEmpty)
        break; // Found the empty line, go read JSON
      else
        lineIsEmpty = true;
    }
    else
      lineIsEmpty = false;  // chracter is not '\n' or '\r'

    //    Serial.print(c);
    delay(10);
  }

  // Read the JSON
  response = "";
  while (A9G.available())
  {
    char c = A9G.read();
    response += c;
    //    Serial.print(c);
    delay(10);
  }

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