We get a json file from a server and we can display it on the serial monitor. But when it comes to extracting the values of interest of this json file, we are blocked. By the way, we can't use these information in our code. For example, we have in it the age of a person and we would like to use this value in an equation.
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
char serverAddress[] = "x";
int port = 8080;
WiFiClient wifi;
HttpClient client= HttpClient(wifi, serverAddress, port);
int wl_status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
while ( wl_status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
wl_status = WiFi.begin(ssid, pass);
}
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
client.println();
}
void loop() {
Serial.println("making GET request");
String request = "/demo/patient/3"; //url of what we need on our server
client.beginRequest();
client.get(request);
client.endRequest();
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.println(statusCode);
//Serial.println(response);
//Serial.println(response);
Serial.flush();
DynamicJsonDocument doc(450);
char json[] = response ;
// 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;
}
const char* id = doc["id"];
double height= doc["height"];
double age = doc["age"];
double weight = doc["weight"];
// Print values.
Serial.println(id);
Serial.println(height);
Serial.println(age);
Serial.println(weight);
delay(15000);
Please do not post partial code, it is a waste of peoples time becuase they have to guess the missing parts and write unecessary questions, and: Which hardware is used? What is the error you get?
I have added the beginning of the code but it isn't really useful here.
We use an MKR wifi 1010 board. Our problem is that we can't use the data in the Json file we import in arduino.
When we try to put the json data in char json[] it tells us that the size of the Json can't be determine.
We think the problem is that there are " in the file and we should have " in order to put all data in a char. But we don't know how to put \ before all " or if it exists another solution.
Because if we write the file by hand and with " instead of " in it, it works
(For example : json[] = {"id": 6,"name": "Smith"} didn't work but json[] = "id": 6,"name": "Smith"} works. )
Based on you original post, your problem was impossible for anyone to solve. Yes, every single " must be escaped, put the JSON in a text editor, replace " with " and copy-paste the result into the source code.
Thank you. But the problem is that we would like to directly use the Json thanks to an arduino code and not having to rewrite it by hand. Is there a function that allows us to add the string \ before each string " ?
(Because the Json file we receive can change over time and so we can't write it by hand we need our code to run by itself.)
escaping all the quotes in your json is for when you have it embedded in your code with the source. If you are fetching it from a website, this is not necessary.
Same thing. You only need to escape the quotes to satisfy the compiler, if you're getting it from somewhere else there is no need. Can you write a test that just gets it from your server and prints it?
We can get and print the Json on the serial but we haven't managed to escape the quotes with a function in order to be able to extract the data in the code.