ArduinoJson Version 6- How do I extract a char array?[SOLVED]

I am using the demo from the library, and modified it with my data. I am able to extract most of the data, except for the text string for weather.

Here is the code:

#include <ArduinoJson.h>

void setup() {
  Serial.begin(115200);
  while (!Serial) continue;
  Serial.println();

  // Allocate the JSON document
  //
  // Inside the brackets, 600 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<600> doc;

  // 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[] = "{\"coord\":{\"lon\":-71.45,\"lat\":42.56},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"base\":\"stations\",\"main\":{\"temp\":52.41,\"pressure\":1024,\"humidity\":81,\"temp_min\":50,\"temp_max\":55},\"visibility\":14484,\"wind\":{\"speed\":5.82,\"deg\":360},\"rain\":{},\"clouds\":{\"all\":90},\"dt\":1572291729,\"sys\":{\"type\":1,\"id\":3530,\"country\":\"US\",\"sunrise\":1572261244,\"sunset\":1572299095},\"timezone\":-14400,\"id\":4955219,\"name\":\"Westford\",\"cod\":200}";


  // 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.
  double temperature = doc["main"]["temp"];
  long pressure = doc["main"]["pressure"];
  int humidity = doc["main"]["humidity"];
  int tempMin = doc["main"]["temp_min"];
  int tempMax = doc["main"]["temp_max"];
  double latitude = doc["coord"]["lat"];
  double longitude = doc["coord"]["lon"];
  const char* weather = doc["weather"]["description"];
  


  // Print values.
  Serial.println();
  Serial.print(F("Temperature: "));
  Serial.println(temperature);
  Serial.print(F("Pressure: "));
  Serial.println(pressure);
  Serial.print(F("Temp min: "));
  Serial.println(tempMin);
  Serial.print(F("Temp max: "));
  Serial.println(tempMax);
  Serial.print(F("Latitude: "));
  Serial.println(latitude);
  Serial.print(F("Longitude: "));
  Serial.println(longitude);
  Serial.print(F("Weather: "));
  Serial.println(weather);


}

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

What am I doing wrong with the code extracting weather?

Here is the json data formatted:

{
   "coord":{
      "lon":-71.45,
      "lat":42.56
   },
   "weather":[
      {
         "id":500,
         "main":"Rain",
         "description":"light rain",
         "icon":"10d"
      }
   ],
   "base":"stations",
   "main":{
      "temp":52.41,
      "pressure":1024,
      "humidity":81,
      "temp_min":50,
      "temp_max":55
   },
   "visibility":14484,
   "wind":{
      "speed":5.82,
      "deg":360
   },
   "rain":{

   },
   "clouds":{
      "all":90
   },
   "dt":1572291729,
   "sys":{
      "type":1,
      "id":3530,
      "country":"US",
      "sunrise":1572261244,
      "sunset":1572299095
   },
   "timezone":-14400,
   "id":4955219,
   "name":"Westford",
   "cod":200
}
1 Like

Last time I looked it the JSON library was using String objects to do its thing. You may find that a String (I can't believe I'm typing this) works better than char*.

wildbill:
Last time I looked it the JSON library was using String objects to do its thing. You may find that a String (I can't believe I'm typing this) works better than char*.

Thanks for the reply.

The original sample code parsed this json object:

char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";

Which formats to:

{ 
   "sensor":"gps",
   "time":1351824120,
   "data":[ 
      48.756080,
      2.302038
   ]
}

And the extract is to a char array:

const char* sensor = doc["sensor"];

Even if the library is returning a String, shouldn't I get something besides null?
I am using Version 6. could the String class return be in prior versions?

[Solved]
I overlooked the assistant tool on the site. I enter a sample of the json object and the assistant writes the code I need.

I wish all libraries were that helpful.

const char* weather = doc["weather"]["description"]; is wrong. I needed to make another object for weather:

JsonObject weather = doc["weather"][0];
  const char* wx_description = weather["description"]; // "light rain"