aREST
aREST copied to clipboard
Is it possible to expose Json ?
Hi there, I'm trying to return json string with aRest on an ESP32S but i get an error about my json format :
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 13 of the JSON data
When i access to my variable by : local.arduino/json
{"json": "{"id":"1","name":"json hub","model":"gp1","firmware":"1","IPAddress":"192.168.0.38","macAddress":"AA:BB:CC:DD:EE","serialNumber":"12345678","ssid":"my_ssid"}", "id": "1", "name": "hub", "hardware": "esp32", "connected": true}
The "
around my json, string is the problem.
So i'd like to know the best way for expose json content with aRest.... i don't have any problem for other data-type formats.
Sample code :
int temperature;
float humidity;
String string;
String json;
void setup()
{
Serial.begin(115200);
if((ssid != NULL) && (ssid[0] != '\0')){
wifi_Connect(ssid, pwd);
}
// Start the server
server.begin();
Serial.println("Server started");
Serial.println("The API is listenning port 80");
// Set the title
rest.title("Interface");
// Give name and ID to device
rest.set_id("1");
rest.set_name("hub");
// Init variables and expose them to REST API
temperature = 26;
humidity = 39.1;
string = "hello world";
json = "";
Serial.println("this is JsonHub function");
StaticJsonBuffer <1000> JSonBufferHub;
JsonObject& root3 = JSonBufferHub.createObject();
root3["id"] = "1";
root3["name"] = "hub";
root3["model"] = "gp1";
root3["firmware"] = "1";
root3["IPAddress"] = "192.168.0.38";
root3["macAddress"] = "AA:BB:CC:DD:EE";
root3["serialNumber"] = "12345678";
root3["ssid"] = ssid;
root3.printTo(json);
Serial.print(F("json: "));
root3.printTo(Serial);
Serial.println();
rest.variable("temperature", &temperature);
rest.variable("humidity", &humidity);
rest.variable("string", &string);
rest.variable("json", &json);
rest.label("temperature");
rest.label("humidity");
rest.label("string");
rest.label("json");
}
Hi, you are definitely doing it the right way! I think one of the recent pull request solved this issue, do you still have the problem?
Hi @marcoschwartz, i'll retest soon
I've test today with platformIO on VSCode with ESP32S to expose json with aRest (not aRest_UI) and i get this kind of json
"{\"id\":1,\"name\":\"my hub\",\"model\":\"gp1\",\"firmware\":\"1\",\"IPAddress\":\"192.168.0.38\",\"macAddress\":\"AA:BB:CC:DD:EE\",\"serialNumber\":\"12345678\",\"ssid\":\"my_ssid\"}"
Hi again, I was diving into this and couldn't reproduce the issue (maybe some problem with VSCode?). Here is the JSON output from an ESP8266 (ESP32 is the same) with the latest version of aREST:

Yeah! I got the same. But my json is expose like a variable returned by function using ArduinoJson library and he was integrate the \ (backslash) before each " (double quote)
{
"variables": {
"temperature": 24,
"humidity": 40,
"hub": "{\"name\":\"update hub data from json api\",\"model\":\"gp1\",\"firmware\":\"1\",\"ip_address\":\"192.168.0.38\",\"mac_address\":\"AA:BB:CC:KK:EE\",\"serial_number\":\"87654321\",\"ssid\":\"CASALHXBROS\"}"
},
"id": "123456",
"name": "greenponik hub",
"hardware": "esp32",
"connected": true
}
I have the same problem. In serial monitor everything look like it's suppose to do, bat when iI access to theexposed variable I get this " instead of just " .
Shouldn't the JSON response have the variable values and the true status for connected inside quotes("")?
I was having a problem parsing the JSON response, so I modified aREST to add the quotes, and I am no longer having the issue.
I changed made the changes here:
void addVariableToBuffer(uint8_t index) {
addStringToBuffer(variable_names[index], true);
addToBufferF(F(": \""));
variables[index]->addToBuffer(this);
addToBufferF(F("\""));
}
void addHardwareToBuffer() {
addToBufferF(F("\"id\": "));
addStringToBuffer(id.c_str(), true);
addToBufferF(F(", \"name\": "));
addStringToBuffer(name, true);
addToBufferF(F(", \"hardware\": "));
addStringToBuffer(HARDWARE, true);
addToBufferF(F(", \"connected\": \"true\"}"));
}
@blackieBlack I am not sure I fully understand, you want quotes also around Booleans like true? According to the JSON specification there should not be: http://ubjson.org/type-reference/value-types/#boolean
Hello,
I'm actually facing the same issue, working with an ESP32.
Here is the output in the Serial monitor:
json: {"id":"1","name":"hub","model":"gp1","firmware":"1","IPAddress":"192.168.0.38","macAddress":"AA:BB:CC:DD:EE","serialNumber":"12345678","ssid":"TC_Eric"}
And now in the web browser:
{"json": "{\"id\":\"1\",\"name\":\"hub\",\"model\":\"gp1\",\"firmware\":\"1\",\"IPAddress\":\"192.168.0.38\",\"macAddress\":\"AA:BB:CC:DD:EE\",\"serialNumber\":\"12345678\",\"ssid\":\"TC_Eric\"}", "id": "1", "name": "hub", "hardware": "esp32", "connected": true}
Adding RawJson() before a value removes the escape characters from the value's double quote, but not the key.
Old code:
root3["id"] = "1";
New code:
root3["id"] = RawJson("1");
Result in the browser (notice how "id":"1" changed) :
{"json": "{\"id\":1,\"name\":\"hub\",\"model\":\"gp1\",\"firmware\":\"1\",\"IPAddress\":\"192.168.0.38\",\"macAddress\":\"AA:BB:CC:DD:EE\",\"serialNumber\":\"12345678\",\"ssid\":\"TC_Eric\"}", "id": "1", "name": "hub", "hardware": "esp32", "connected": true}
If I'm not mistaken, this Json payload is stored as a string, so it should be possible to modify aREST in order to remove all escape quotes from the string before returning it to the browser.
If what I (optimistically) stated above is correct, could any of you please point me toward the part of aREST's code I should modify for that?
Thanks!