Get data outoff String/Daten aus einem String herausnehmen

English version:
Hello,
I have a String that contains the power consumption of my house it looks something like that:

{"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}}

to get this string i coded this on my esp32:

#include <WiFi.h>
#include <HTTPClient.h>
  
const char* ssid = "REMOVED";
const char* password =  "REMOVED";
  
void setup() {
  
  Serial.begin(115200);
  delay(4000);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  
  Serial.println("Connected to the WiFi network");
  
}
  
void loop() {
  
  if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
  
    HTTPClient http;
  
    http.begin("http://192.168.178.85/rpc/Shelly.GetStatus"); //Specify the URL
    int httpCode = http.GET();                                        //Make the request
  
    if (httpCode > 0) { //Check for the returning code
  
        String payload = http.getString();
        Serial.println(httpCode);
        Serial.println(payload);
      }
  
    else {
      Serial.println("Error on HTTP request");
    }
  
    http.end(); //Free the resources
  }
  
  delay(1000);  
}

It connects with my Wifi and does http.get to get that string. Now i want to store the values for every "power information" in its own variabel. Online i have read something about "strtok" but i dont really know/understand how to use it. Could somebody help me? Thanks infront.

German version:
Hallo,
ich habe einen String aus dem ich gerne daten herausnehmen möchte er enthält den verbrauch meines Hauses. (String von oben)
Als Gerät benutze ich den esp32 und habe im voraus um den String zu bekommen den code von oben programmiert.
Nun möchte ich aus dem String die Daten herausnehmen und in ihre eigen Variable speichern, sodass ich mit der Variable weiter Programmieren kann. Online habe ich was von "strtok" gelesen, habe das ganze aber nicht wirklich verstanden. Vielleicht gibt es einen einfacheren Weg die Variablennamen und die floats aus dem String herauszubekommen. Danke im Voraus.

Welcome

Use this library

you could certainly use the string tokeniser, e.g.

// strtok example - parse tokens

void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200);
    char str[] = R"rawliteral({"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}})rawliteral";    // text to tokenise
    char * pch;                     // pointer to tokens
    Serial.print("Splitting string ");
    Serial.print(str);
    Serial.println(" into tokens:");
    pch = strtok (str,"{}}, :\"");         // get first token
    while (pch != NULL)
      {
      Serial.print("string found ");
      Serial.println(pch);          // print it
      pch = strtok (NULL, "{}}, :\"");     // get next token
     }  
}

void loop() {}

when run gives

Splitting string {"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}} into tokens:
string found ble
string found cloud
string found connected
string found true
string found em
string found 0
string found id
string found 0
string found a_current
string found 0.284
string found a_voltage
string found 231.3
string found a_act_power
string found 39.0
string found a_aprt_power
string found 65.6
string found a_pf
string found 0.71
string found a_freq
string found 50.0
string found b_current
string found 0.750
string found b_voltage
string found 228.1
string found b_act_power
string found 114.9
string found b_aprt_power
string found 171.0
string found b_pf
string found 0.67
string found b_freq
string found 50.0
string found c_current
string found 1.335
string found c_voltage
string found 225.7
string found c_act_power
string found 247.6
string found c_aprt_power
string found 300.7
string found c_pf
string found 0.82
string found c_freq
string found 50.0
string found n_current
string found null
string found total_current
string found 2.368
string found total_act_power
string found 401.539
string found total_aprt_power
string found 537.280
string found user_calibrated_phase
string found []
string found emdata
string found 0
string found id
string found 0
string found a_total_act_energy
string found 47449.04
string found a_total_act_ret_energy
string found 0.00
string found b_total_act_energy
string found 29757.56
string found b_total_act_ret_energy
string found 0.00
string found c_total_act_energy
string found 48733.79
string found c_total_act_ret_energy
string found 0.00
string found total_act
string found 125940.39
string found total_act_ret
string found 0.00
string found eth
string found ip
string found null
string found modbus
string found mqtt
string found connected
string found false
string found sys
string found mac
string found 34987A687370
string found restart_required
string found false
string found time
string found 17
string found 30
string found unixtime
string found 1704385844
string found uptime
string found 1047885
string found ram_size
string found 241352
string found ram_free
string found 103720
string found fs_size
string found 524288
string found fs_free
string found 184320
string found cfg_rev
string found 20
string found kvs_rev
string found 0
string found schedule_rev
string found 0
string found webhook_rev
string found 0
string found available_updates
string found reset_reason
string found 3
string found temperature
string found 0
string found id
string found 0
string found tC
string found 44.5
string found tF
string found 112.1
string found wifi
string found sta_ip
string found REMOVED
string found status
string found got
string found ip
string found ssid
string found REMOVED
string found rssi
string found -77
string found ws
string found connected
string found false

you would have to look at each token
e.g. if you find a_current you know the next toen is a value 0.284

as it looks like JSON you could try decoding-and-encoding-json

Thank you. I will look in to it. If there is anyting else let me know. Ty

using JSON parser on part of your JSON

// JsonParserExample

// from https://arduinojson.org/v6/example/parser/


#include <ArduinoJson.h>

// Allocate the JSON document
//
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<1000> doc;

// StaticJsonDocument<N> allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
//
// DynamicJsonDocument doc(1000);
// JSON input string.
//
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// the minimal amount of memory because the JsonDocument stores pointers to
// the input buffer.
// If you use another type of input, ArduinoJson must copy the strings from
// the input to the JsonDocument, so you need to increase the capacity of the
// JsonDocument.
char json[] =  //"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  //R"rawliteral({"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}})rawliteral";    // text to tokenise
  R"rawliteral({"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]})rawliteral";  // text to tokenise

void setup() {
  // Initialize serial port
  Serial.begin(115200);
  while (!Serial) continue;
  Serial.printf("\n\ndeserializeJson()  JSON\n");

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, json);

  // Test if parsing succeeds.
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    while (1) delay(1000);
  }
  Serial.println(F("deserializeJson() OK "));

  // Fetch values.
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do doc["time"].as<long>();
  double a_current = doc["a_current"];
  double a_voltage = doc["a_voltage"];
  double a_act_power = doc["a_act_power"];

  // Print values.
  Serial.print("a_current ");
  Serial.println(a_current);
  Serial.print("a_voltage ");
  Serial.println(a_voltage);
  Serial.print("a_act_power ");
  Serial.println(a_act_power);
}

void loop() {
  // not used in this example
}

displays on an ESP32

deserializeJson()  JSON
deserializeJson() OK 
a_current 0.28
a_voltage 231.30
a_act_power 39.00

Hello guys,
the last days i sat down and try to take your tipps and convert that in something. But i didn't get around the problem that i couldn't convert the String i had into the char str/ char Json to work with ur ideas. So i looked deeper into the documentation on Strings and found substrings. It's probably not the best solution but it works for now. Also it's pretty easy so I can understand it. My Code in the end looks like this. It's not finished and im still working on it. If anyone has suggestions how to improve it I gladly take it. For anyone intressted the consumption-data is coming frome a Shelly Pro 3EM.

#include <WiFi.h>
#include <HTTPClient.h>
  
const char* ssid = "REMOVED";
const char* password =  "REMOVED";

String Data;


float a_current;
float a_act_power;

float b_current;
float b_act_power;

float c_current;
float c_act_power;

  
void setup() {
  
  Serial.begin(115200);
  delay(4000);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  
  Serial.println("Connected to the WiFi network");
  
}
  
void loop() {
  
  if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
  
    HTTPClient http;
  
    http.begin("http://192.168.REMOVED/rpc/Shelly.GetStatus"); //Specify the URL
    int httpCode = http.GET();                                        //Make the request
  
    if (httpCode > 0) { //Check for the returning code
  
      String payload = http.getString();
      Serial.println(payload);
      Data = payload;
    }
  
    else {
      Serial.println("Error on HTTP request");
    }
  
    http.end(); //Free the resources
  }

  PhaseEinz();
  PhaseZwei();
  PhaseDrei();
  
  delay(3000);  
}


void PhaseEinz(){

  int a_currentIndex = Data.indexOf("a_current");
  if(a_currentIndex == 53){//cheking if string is still the same and nothing changed
    String SUB_a_current = Data.substring(64, 69);
    a_current = SUB_a_current.toFloat();
    Serial.print("Der Strom auf Phase Einz ist:");
    Serial.println(a_current);
  }else{
    Stop();
  }


  int a_powerIndex = Data.indexOf("a_act_power");
  if(a_powerIndex == 89){
    String SUB_a_act_power = Data.substring(102, 107);
    a_act_power = SUB_a_act_power.toFloat();
    Serial.print("Die Leistung auf Phase Einz ist:");
    Serial.println(a_act_power);
  }else{
    Stop();
  }
}

void PhaseZwei(){
  
  int b_currentIndex = Data.indexOf("b_current");
  if(b_currentIndex == 154){
    String SUB_b_current = Data.substring(165, 170);
    b_current = SUB_b_current.toFloat();
    Serial.print("Der Strom auf Phase Zwei ist:");
    Serial.println(b_current);
  }else{
    Stop();
  }

  int b_powerIndex = Data.indexOf("b_act_power");
  if(b_powerIndex == 190){
    String SUB_b_act_power = Data.substring(203, 208);
    b_act_power = SUB_b_act_power.toFloat();
    Serial.print("Die Leistung auf Phase Zwei ist:");
    Serial.println(b_act_power);
  }else{
    Stop();
  }
}

void PhaseDrei(){

  int c_currentIndex = Data.indexOf("c_current");
  if(c_currentIndex == 255){
    String SUB_c_current = Data.substring(266, 271);
    c_current = SUB_c_current.toFloat();
    Serial.print("Der Strom auf Phase Drei ist:");
    Serial.println(c_current);
  }else{
    Stop();
  }

  int c_powerIndex = Data.indexOf("c_act_power");
  if(c_powerIndex == 291){
    String SUB_c_act_power = Data.substring(304, 309);
    c_act_power = SUB_c_act_power.toFloat();
    Serial.print("Die Leistung auf Phase Drei ist:");
    Serial.println(c_act_power);
  }  else{
    Stop();
  }
}


void Stop(){//emergency stop if something happens


}

if your received JSON is in a String you can use the String.c_str() function to give you access to a C type char array which you can pass to the JSON deserializeJson() function

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, jsonString.c_str());

full code

// JsonParserExample

// from https://arduinojson.org/v6/example/parser/


#include <ArduinoJson.h>

// Allocate the JSON document
//
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<1000> doc;

// StaticJsonDocument<N> allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
//
// DynamicJsonDocument doc(1000);
// JSON input string.
//
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// the minimal amount of memory because the JsonDocument stores pointers to
// the input buffer.
// If you use another type of input, ArduinoJson must copy the strings from
// the input to the JsonDocument, so you need to increase the capacity of the
// JsonDocument.
String jsonString =  //"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  //R"rawliteral({"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}})rawliteral";    // text to tokenise
  R"rawliteral({"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]})rawliteral";  // text to tokenise

void setup() {
  // Initialize serial port
  Serial.begin(115200);
  while (!Serial) continue;
  Serial.printf("\n\ndeserializeJson()  JSON\n");

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, jsonString.c_str());

  // Test if parsing succeeds.
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    while (1) delay(1000);
  }
  Serial.println(F("deserializeJson() OK "));

  // Fetch values.
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do doc["time"].as<long>();
  double a_current = doc["a_current"];
  double a_voltage = doc["a_voltage"];
  double a_act_power = doc["a_act_power"];

  // Print values.
  Serial.print("a_current ");
  Serial.println(a_current);
  Serial.print("a_voltage ");
  Serial.println(a_voltage);
  Serial.print("a_act_power ");
  Serial.println(a_act_power);
}

void loop() {
  // not used in this example
}

serial monitor output

deserializeJson()  JSON
deserializeJson() OK 
a_current 0.28
a_voltage 231.30
a_act_power 39.00

thinking again as your JSON is in a String you can select substrings to deserialise
this code selects a substring from the your original JSON and deserialises it

// JsonParserExample

// from https://arduinojson.org/v6/example/parser/


#include <ArduinoJson.h>

// Allocate the JSON document
//
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<1000> doc;

// StaticJsonDocument<N> allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
//
// DynamicJsonDocument doc(1000);
// JSON input string.
//
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// the minimal amount of memory because the JsonDocument stores pointers to
// the input buffer.
// If you use another type of input, ArduinoJson must copy the strings from
// the input to the JsonDocument, so you need to increase the capacity of the
// JsonDocument.
String jsonString = 
  R"rawliteral({"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}})rawliteral";    // text to tokenise

void setup() {
  // Initialize serial port
  Serial.begin(115200);
  while (!Serial) continue;
  Serial.printf("\n\ndeserializeJson()  JSON\n");
  Serial.printf("original JSON = %s\n\n",jsonString.c_str());
  // look for JSON string starting with {"id
  String em_0=jsonString.substring(jsonString.indexOf("{\"id"));//, jsonString.indexOf("},","em:0:")))
  Serial.printf("JSON starting with {\"id = %s \n",em_0.c_str());
  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, em_0.c_str());

  // Test if parsing succeeds.
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    while (1) delay(1000);
  }
  Serial.println(F("deserializeJson() OK "));

  // Fetch values.
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do doc["time"].as<long>();
  double a_current = doc["a_current"];
  double a_voltage = doc["a_voltage"];
  double a_act_power = doc["a_act_power"];

  // Print values.
  Serial.print("a_current ");
  Serial.println(a_current);
  Serial.print("a_voltage ");
  Serial.println(a_voltage);
  Serial.print("a_act_power ");
  Serial.println(a_act_power);
}

void loop() {
  // not used in this example
}

serial monitor output

deserializeJson()  JSON
original JSON = {"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}}

JSON starting with {"id = {"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}} 
deserializeJson() OK 
a_current 0.28
a_voltage 231.30
a_act_power 39.00

I assume there must be a simpler way to use ArduinoJson to do the above without extracting substrings????

I will need to have a look into it but its seems pretty appealing. It's propably what I have been searching for. Thank you @horace for you help.

no need to use substring you can fetch values from the JSON doc so

  double a_current = doc["em:0"]["a_current"];

complete program

// JsonParserExample

// from https://arduinojson.org/v6/example/parser/


#include <ArduinoJson.h>

// Allocate the JSON document
//
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<1000> doc;

// StaticJsonDocument<N> allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
//
// DynamicJsonDocument doc(1000);
// JSON input string.
//
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// the minimal amount of memory because the JsonDocument stores pointers to
// the input buffer.
// If you use another type of input, ArduinoJson must copy the strings from
// the input to the JsonDocument, so you need to increase the capacity of the
// JsonDocument.
String jsonString =
  R"rawliteral({"ble":{},"cloud":{"connected":true},"em:0":{"id":0,"a_current":0.284,"a_voltage":231.3,"a_act_power":39.0,"a_aprt_power":65.6,"a_pf":0.71,"a_freq":50.0,"b_current":0.750,"b_voltage":228.1,"b_act_power":114.9,"b_aprt_power":171.0,"b_pf":0.67,"b_freq":50.0,"c_current":1.335,"c_voltage":225.7,"c_act_power":247.6,"c_aprt_power":300.7,"c_pf":0.82,"c_freq":50.0,"n_current":null,"total_current":2.368,"total_act_power":401.539,"total_aprt_power":537.280, "user_calibrated_phase":[]},"emdata:0":{"id":0,"a_total_act_energy":47449.04,"a_total_act_ret_energy":0.00,"b_total_act_energy":29757.56,"b_total_act_ret_energy":0.00,"c_total_act_energy":48733.79,"c_total_act_ret_energy":0.00,"total_act":125940.39, "total_act_ret":0.00},"eth":{"ip":null},"modbus":{},"mqtt":{"connected":false},"sys":{"mac":"34987A687370","restart_required":false,"time":"17:30","unixtime":1704385844,"uptime":1047885,"ram_size":241352,"ram_free":103720,"fs_size":524288,"fs_free":184320,"cfg_rev":20,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{},"reset_reason":3},"temperature:0":{"id": 0,"tC":44.5, "tF":112.1},"wifi":{"sta_ip":"REMOVED","status":"got ip","ssid":"REMOVED","rssi":-77},"ws":{"connected":false}})rawliteral";  // text to tokenise

void setup() {
  // Initialize serial port
  Serial.begin(115200);
  while (!Serial) continue;
  Serial.printf("\n\ndeserializeJson()  JSON\n");

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, jsonString.c_str());

  // Test if parsing succeeds.
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    while (1) delay(1000);
  }
  Serial.println(F("deserializeJson() OK "));

  // Fetch values.
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do doc["time"].as<long>();
  double a_current = doc["em:0"]["a_current"];
  double a_voltage = doc["em:0"]["a_voltage"];
  double a_act_power = doc["em:0"]["a_act_power"];

  // Print values.
  Serial.print("a_current ");
  Serial.println(a_current);
  Serial.print("a_voltage ");
  Serial.println(a_voltage);
  Serial.print("a_act_power ");
  Serial.println(a_act_power);

  double a_total_act_energy = doc["emdata:0"]["a_total_act_energy"];
  Serial.print("a_total_act_energy ");
  Serial.println(a_total_act_energy);
  double b_total_act_energy = doc["emdata:0"]["b_total_act_energy"];
  Serial.print("b_total_act_energy ");
  Serial.println(b_total_act_energy);
}

void loop() {
  // not used in this example
}

serial displays

deserializeJson()  JSON
deserializeJson() OK 
a_current 0.28
a_voltage 231.30
a_act_power 39.00
a_total_act_energy 47449.04
b_total_act_energy 29757.56

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