JSON: Splitting String Value

Hi!
I'm working with an ESP12-F, on getting a payload from an API.
The string that comes when requested looks like the following (which gets printed on the serial monitor):

{"replies":["Pa_1264354_09th9845-rm32-95we-hny0-7u3fv7g4bhc_1_0_EMPLOYEE_"]}

But I am supposed to print it in the following way (to split by the underscore):

{"replies"
:["Pa
1264354
09th9845-rm32-95we-hny0-7u3fv7g4bhc
1
0
EMPLOYEE
"]}

I gathered from websites that I could use Json.parse to do so, but I could not get the required outcome.
Below is the code that I used.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>


const char* ssid = "ssid";//WiFi Network Name
const char* password = "1234";// WiFi password


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

  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi");
  }
  Serial.print("Connected to WiFi");
 
}

void loop() {
  
   
    if(WiFi.status()== WL_CONNECTED){
      String path = "URL provided";
      
      String jsonBuffer = httpGETRequest(path.c_str());
      Serial.println(jsonBuffer);
      JSONVar jsonObject = JSON.parse(jsonBuffer);
  
  
      if (JSON.typeof(jsonObject) == "undefined") {
        Serial.println("Parsing input failed!");
        return;
      }

      Serial.print("JSON object = ");
      Serial.println(jsonObject);

    }
    else {
      Serial.println("WiFi Disconnected");
    }

}

String httpGETRequest(const char* server) {
  WiFiClient client;
  HTTPClient http;
    
  http.begin(client, server);
  
  int httpCode = http.GET();
  
  String payload = "{}"; 
  
  if (httpCode>0) {
    Serial.print("HTTP Code: ");
    Serial.println(httpCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error!");
  }
  // Free resources
  http.end();

  return payload;
}
  

Really appreciate if someone could point out what I have done wrong what I am to do to get the required output.

Thanks in advance!

Shainy Fonseka

Here is a comprehensive guide for working with JSON objects.

To split a string using a delimiter, you can use the indexOf and substring functions of a String object.

1 Like

OP is using Arduino_JSON.h not ArduinoJson's library

but the advice works, you could just replace the '_' by a new line

      String jsonBuffer = httpGETRequest(path.c_str());
      String jsonBufferPretty = jsonBuffer;
      jsonBufferPretty.replace("_", "\n"); // <<== replace underscores by a new line
      Serial.println(jsonBufferPretty); // <<= print it

you can obviously walk through the buffer and if you find the '_' then just print the new line

char jsonBuffer[] = "{\"replies\":[\"Pa_1264354_09th9845-rm32-95we-hny0-7u3fv7g4bhc_1_0_EMPLOYEE_\"]}";

void setup() {
  Serial.begin(115200); Serial.println();
  const char* ptr = jsonBuffer;
  while (*ptr) {
    if (*ptr == '_')  Serial.write('\n');
    else if (*ptr == ':')
    {
      Serial.write(*ptr);
      Serial.write('\n');
    }
    else Serial.write(*ptr);
    ptr++;
  }
}

void loop() {}
1 Like

Thank you @jfjlaros for providing the necessary materials! Much appreciated!

Thank you so much @J-M-L for helping me out! Really appreciate it!

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