Parse Json errors

Hi all

Having trouble extracting the JSON data[1] values and suggestion welcome?
Values are 2764 and 3950
ending up with an error "expected "'" or ";" before cid
I don't think it correctly conforms to a JSON stream?

thanks
Geoff

#include <ArduinoJson.h>

void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial) continue;

// Allocate the JSON document

StaticJsonDocument<200> doc;

char json[] ="[{"cid":"PWER","data":[{"1560131426000":2764}],"sid":"816087","units":"W","age":4},{"cid":"PWER_SUB","data":[{"1560131421000":3950}],"sid":"816190","units":"W","age":9}]";

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

// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}

// Fetch values.

const char* sensor = doc["PWER"];
int Generation = doc["data"][1];

const char* sensor1 = doc["PWER_SUB"];
int Usage = doc["data"][1];

// Print values.
Serial.println(sensor);
Serial.println(sensor1);
Serial.println(Generation);
Serial.println(Usage);

}

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

Go to Assistant | ArduinoJson 6

Paste the JSON into the box titled "Input".

[{"cid":"PWER","data":[{"1560131426000":2764}],"sid":"816087","units":"W","age":4},{"cid":"PWER_SUB","data":[{"1560131421000":3950}],"sid":"816190","units":"W","age":9}]

Page down and see the box titled "Parsing Program" for source code.

const size_t capacity = 2*JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(2) + 2*JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(5) + 120;
DynamicJsonDocument doc(capacity);

const char* json = "[{\"cid\":\"PWER\",\"data\":[{\"1560131426000\":2764}],\"sid\":\"816087\",\"units\":\"W\",\"age\":4},{\"cid\":\"PWER_SUB\",\"data\":[{\"1560131421000\":3950}],\"sid\":\"816190\",\"units\":\"W\",\"age\":9}]";

deserializeJson(doc, json);

JsonObject root_0 = doc[0];
const char* root_0_cid = root_0["cid"]; // "PWER"

int root_0_data_0_1560131426000 = root_0["data"][0]["1560131426000"]; // 2764

const char* root_0_sid = root_0["sid"]; // "816087"
const char* root_0_units = root_0["units"]; // "W"
int root_0_age = root_0["age"]; // 4

JsonObject root_1 = doc[1];
const char* root_1_cid = root_1["cid"]; // "PWER_SUB"

int root_1_data_0_1560131421000 = root_1["data"][0]["1560131421000"]; // 3950

const char* root_1_sid = root_1["sid"]; // "816190"
const char* root_1_units = root_1["units"]; // "W"
int root_1_age = root_1["age"]; // 9

you should use back slash character before double quotes in c string.

char json[] ="[{\"cid\":\"PWER\",\"data\":[{\"1560131426000\":2764}],\"sid\":\"816087\",\"units\":\"W\",\"age\":4},{\"cid\":\"PWER_SUB\",\"data\":[{\"1560131421000\":3950}],\"sid\":\"816190\",\"units\":\"W\",\"age\":9}]";

Ok thanks the assistant is a handy tool
The output is from the Efergy Engage API... only issue I see is that it looks like it is treating ["1560131421000"] and ["1560131426000"] as fixed values but these are time stamps that continually change

Geoff

Yes, I wondered about those large numbers. They are on the left hand side of ':' so they are names of name/value pairs. Maybe the software generating the JSON string needs to be upgraded.