Trouble Parsing JSON from Webhook

Hi all

I’m trying to parse some weather data from OpenWeatherMap.com. I’m getting the JSON into the terminal, but I can’t parse it. Here is the JSON:

{“coord”:{“lon”:-87.65,“lat”:41.85},“weather”:[{“id”:804,“main”:“Clouds”,“description”:“overcast clouds”,“icon”:“04d”}],“base”:“stations”,“main”:{“temp”:302.06,“pressure”:1017,“humidity”:51,“temp_min”:300.15,“temp_max”:305.15},“visibility”:16093,“wind”:{“speed”:5.1,“deg”:260},“clouds”:{“all”:90},“dt”:1505675700,“sys”:{“type”:1,“id”:966,“message”:0.0037,“country”:“US”,“sunrise”:1505648038,“sunset”:1505692478},“id”:4887398,“name”:“Chicago”,“cod”:200}

I have a feeling I’m messing up where I’m doing Serial.printlnf("TempF: ", root["coord"]["lon"].asString()); ,that’s not working. To start, I was just trying to parse the longitude, so then I could, ideally, be able to parse anything like the temp, wind, etc. Any ideas?
Here is my code:

    #include <SparkJson.h>

    int i = 0;
    void myHandler(const char *event, const char *data)
    {
      i++;
      Serial.print(i);
      Serial.print(event);
      Serial.print(", data: ");
      if (data)
        Serial.println(data);
      else
        Serial.println("NULL");
        
        const char *rawJsonData = data;
        	char *dataCopy = strdup(rawJsonData);
    parseWeather(dataCopy);

    }

    void parseWeather(char *data); // forward declaration

    void setup() {
      Particle.subscribe("hook-response/temperature", myHandler, MY_DEVICES);
      Serial.begin(9600);
    }
void loop(){
delay(1000);
}

void parseWeather(char *mutableData) {
	StaticJsonBuffer<1000> jsonBuffer;
	JsonObject& root = jsonBuffer.parseObject(mutableData);

	if (!root.success()) {
		Serial.println("parseObject() failed");
	    return;
	}

	Serial.printlnf("TempF: ", root["coord"]["lon"].asString());

}

Hey @rickkas7

I’m trying to parse some weather data from OpenWeatherMap.com. I’m getting the JSON into the terminal, but I can’t parse it. Here is the JSON:

{“coord”:{“lon”:-87.65,“lat”:41.85},“weather”:[{“id”:804,“main”:“Clouds”,“description”:“overcast clouds”,“icon”:“04d”}],“base”:“stations”,“main”:{“temp”:302.06,“pressure”:1017,“humidity”:51,“temp_min”:300.15,“temp_max”:305.15},“visibility”:16093,“wind”:{“speed”:5.1,“deg”:260},“clouds”:{“all”:90},“dt”:1505675700,“sys”:{“type”:1,“id”:966,“message”:0.0037,“country”:“US”,“sunrise”:1505648038,“sunset”:1505692478},“id”:4887398,“name”:“Chicago”,“cod”:200}

Here is my code: I used your suggestion for the most part, but I have a feeling it’s where I’m doing Serial.printlnf("TempF: ", root["coord"]["lon"].asString()); that’s not working. Any ideas?

#include <SparkJson.h>

int i = 0;
void myHandler(const char *event, const char *data)
{
  i++;
  Serial.print(i);
  Serial.print(event);
  Serial.print(", data: ");
  if (data)
    Serial.println(data);
  else
    Serial.println("NULL");
    
    const char *rawJsonData = data;
    	char *dataCopy = strdup(rawJsonData);
parseWeather(dataCopy);

}

void parseWeather(char *data); // forward declaration

void setup() {
  Particle.subscribe("hook-response/temperature", myHandler, MY_DEVICES);
  Serial.begin(9600);
}

void loop(){
delay(1000);
}
void parseWeather(char *mutableData) {
	StaticJsonBuffer<1000> jsonBuffer;
	JsonObject& root = jsonBuffer.parseObject(mutableData);
	if (!root.success()) {
		Serial.println("parseObject() failed");
	    return;
	}
	Serial.printlnf("TempF: ", root["coord"]["lon"].asString());
}

@JackD12, double posting is sternly frowned upon - don’t do it.

@ScruffR, Sorry I didn’t realize that everybody was notified of the first post. It won’t happen again.Do you have any ideas…:wink: