Parse JSON

Hi all i'm trying to parse the JSON string that comes from my Fronius inverter to extract the power being produced value but unable to come up with anything that isolates the value
Is some one able to enlighten me on what the code should look like to extract the red font "PAR" Value out?
I've stored the json in a String..

Thanks
Geoff

{
"Body" : {
"Data" : {
"DAY_ENERGY" : {
"Unit" : "Wh",
"Value" : 60.799999999999997
},
"DeviceStatus" : {
"ErrorCode" : 0,
"LEDColor" : 2,
"LEDState" : 0,
"MgmtTimerRemainingTime" : -1,
"StateToReset" : false,
"StatusCode" : 7
},
"FAC" : {
"Unit" : "Hz",
"Value" : 50.079999999999998
},
"IAC" : {
"Unit" : "A",
"Value" : 0.63
},
"IDC" : {
"Unit" : "A",
"Value" : 0.48999999999999999
},
"PAC" : {
"Unit" : "W",
"Value" : 156
},
"TOTAL_ENERGY" : {
"Unit" : "Wh",
"Value" : 377835.03000000003
},
"UAC" : {
"Unit" : "V",
"Value" : 248.40000000000001
},
"UDC" : {
"Unit" : "V",
"Value" : 348.10000000000002
},
"YEAR_ENERGY" : {
"Unit" : "Wh",
"Value" : 377835
}
}
},
"Head" : {
"RequestArguments" : {
"DataCollection" : "CommonInverterData",
"DeviceClass" : "Inverter",
"DeviceId" : "1",
"Scope" : "Device"
},
"Status" : {
"Code" : 0,
"Reason" : "",
"UserMessage" : ""
},
"Timestamp" : "2018-07-13T07:26:31+10:00"
}
}

Are you trying with such a library?: GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficie

yes trying that but just cant get my head around it using as below but just get a "0" back

String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload

DynamicJsonBuffer jsonBuffer(200);

JsonObject& root = jsonBuffer.parseObject(payload);
if (!root.success()) {
Serial.println("parseObject() failed");
}

int PAC = root["PAC"];
Serial.println(PAC);

Geoff

What's this?

{
"Body" : {
"Data" : {
"DAY_ENERGY" : {
"Unit" : "Wh",
"Value" : 60.799999999999997
},
"DeviceStatus" : {
"ErrorCode" : 0,
"LEDColor" : 2,
"LEDState" : 0,
"MgmtTimerRemainingTime" : -1,
"StateToReset" : false,
"StatusCode" : 7
},
"FAC" : {
"Unit" : "Hz",
"Value" : 50.079999999999998
},
"IAC" : {
"Unit" : "A",
"Value" : 0.63
},
"IDC" : {
"Unit" : "A",
"Value" : 0.48999999999999999
},
"PAC" : {
"Unit" : "W",
"Value" : 156
},
"TOTAL_ENERGY" : {
"Unit" : "Wh",
"Value" : 377835.03000000003
},
"UAC" : {
"Unit" : "V",
"Value" : 248.40000000000001
},
"UDC" : {
"Unit" : "V",
"Value" : 348.10000000000002
},
"YEAR_ENERGY" : {
"Unit" : "Wh",
"Value" : 377835
}
}
},
"Head" : {
"RequestArguments" : {
"DataCollection" : "CommonInverterData",
"DeviceClass" : "Inverter",
"DeviceId" : "1",
"Scope" : "Device"
},
"Status" : {
"Code" : 0,
"Reason" : "",
"UserMessage" : ""
},
"Timestamp" : "2018-07-13T07:26:31+10:00"
}
}

geoff_g:
I've stored the json in a String..

Are you using the string class?

And, post your whole code.

Ok code as below - it gets the inverter sting fine - but from then on messy…

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = “mine”;
const char* password = “12345”;

void setup () {

Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);
Serial.println(“Connecting…”);

}
Serial.println(“Connected…”);
}

void loop() {

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

HTTPClient http; //Declare an object of class HTTPClient

http.begin(“http://192.168.1.111/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData”); //Specify request destination
int httpCode = http.GET(); //Send the request

String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload

const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
Serial.println(“JSON covert…”);

JsonObject& root = jsonBuffer.parseObject(payload);
if (!root.success()) {
Serial.println(“parseObject() failed”);
}

Serial.println(“JSON print…”);
int PAC = root[“PAC”];
Serial.println(PAC);

http.end(); //Close connection

}

delay(30000); //Send a request every 30 seconds

}

I tried your JSON string here: [Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ

// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
//
// This example shows how to deserialize a JSON document with ArduinoJson.

#include <iostream>
#include "ArduinoJson.h"


int main()
{
  // Memory pool for JSON object tree.
  //
  // Inside the brackets, 200 is the size of the pool in bytes,
  // If the JSON object is more complex, you need to increase that value.
  // See https://bblanchon.github.io/ArduinoJson/assistant/
  StaticJsonBuffer<3000> jsonBuffer;

  // StaticJsonBuffer allocates memory on the stack, it can be
  // replaced by DynamicJsonBuffer which allocates in the heap.
  //
  // DynamicJsonBuffer  jsonBuffer(200);

  // JSON input string.
  //
  // It's better to use a char[] as shown here.
  // If you use a const char* or a String, ArduinoJson will
  // have to make a copy of the input in the JsonBuffer.
  char json[] =
      R"(
{
   "Body" : {
      "Data" : {
         "DAY_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 60.799999999999997
         },
         "DeviceStatus" : {
            "ErrorCode" : 0,
            "LEDColor" : 2,
            "LEDState" : 0,
            "MgmtTimerRemainingTime" : -1,
            "StateToReset" : false,
            "StatusCode" : 7
         },
         "FAC" : {
            "Unit" : "Hz",
            "Value" : 50.079999999999998
         },
         "IAC" : {
            "Unit" : "A",
            "Value" : 0.63
         },
         "IDC" : {
            "Unit" : "A",
            "Value" : 0.48999999999999999
         },
         "PAC" : {
            "Unit" : "W",
            "Value" : 156
         },
         "TOTAL_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 377835.03000000003
         },
         "UAC" : {
            "Unit" : "V",
            "Value" : 248.40000000000001
         },
         "UDC" : {
            "Unit" : "V",
            "Value" : 348.10000000000002
         },
         "YEAR_ENERGY" : {
            "Unit" : "Wh",
            "Value" : 377835
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DataCollection" : "CommonInverterData",
         "DeviceClass" : "Inverter",
         "DeviceId" : "1",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2018-07-13T07:26:31+10:00"
   }
}
)" ;

  // Root of the object tree.
  //
  // It's a reference to the JsonObject, the actual bytes are inside the
  // JsonBuffer with all the other nodes of the object tree.
  // Memory is freed when jsonBuffer goes out of scope.
  JsonObject& root = jsonBuffer.parseObject(json);

  // Test if parsing succeeds.
  if (!root.success()) {
    std::cerr << "parseObject() failed" << std::endl;
    return 1;
  }

  // Fetch values.
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do root["time"].as<long>();
  // const char* sensor = root["sensor"];
  // long time = root["time"];
  // double latitude = root["data"][0];
  // double longitude = root["data"][1];

  // Print values.
  std::cout << root["Body"]["Data"]["PAC"]["Value"] << std::endl;
  // std::cout << time << std::endl;
  // std::cout << latitude << std::endl;
  // std::cout << longitude << std::endl;
  
  return 0;
}

This is how you de-nest to get your desired value:

root["Body"]["Data"]["PAC"]["Value"]

Try this...(not tested)

JsonObject& root = jsonBuffer.parseObject(payload);
JsonObject& Body_Data = root["Body"]["Data"];
                  if (!root.success()) {
                      Serial.println("parseObject() failed");
                     }
                
        Serial.println("JSON print..");
          int PAC = Body_Data["PAC"]["Value"];
            Serial.println(PAC);

    
    http.end();   //Close connection

G

Thanks Zardof
That works just great and now i see where i was going wrong

Geoff