String from a JSON data line

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
EBCrp
 
Posts: 16
Joined: Fri Nov 11, 2022 6:19 pm

String from a JSON data line

Post by EBCrp »

Is there a trick to pulling a string out of a JSON data line vs. a numerical variable? I am pulling information from openweathermap.org. I won't give you all the code because it is working ok but I cannot get the "[weather][main]" line to work and I can't seem to figure out the error:

double temp = (double) api_object["main"]["temp"];
String cond = api_object["weather"]["main"];

the temp line works just fine, matter of fact any line with numbers is working but I can't get the strings to come over and I have tried almost everything I can find in the JSON.h library. Is it my use of "String"? If someone can provide a nudge that would be great!!

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: String from a JSON data line

Post by dastels »

What JSON library are you using?

Dave

User avatar
EBCrp
 
Posts: 16
Joined: Fri Nov 11, 2022 6:19 pm

Re: String from a JSON data line

Post by EBCrp »

#include <Arduino_JSON.h>

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: String from a JSON data line

Post by dastels »

What if you used const char* rather than String?

Dave

User avatar
EBCrp
 
Posts: 16
Joined: Fri Nov 11, 2022 6:19 pm

Re: String from a JSON data line

Post by EBCrp »

I get a blank response .

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: String from a JSON data line

Post by dastels »

What is the jason that you are extracting the data from? Specifically the ["weather"]["main"] path.

Dave

User avatar
EBCrp
 
Posts: 16
Joined: Fri Nov 11, 2022 6:19 pm

Re: String from a JSON data line

Post by EBCrp »

I have attached part of the sketch and also part of the serial monitor response (at the bottom) with what I think you are asking for. Thanks for your help!!
Attachments
JSON File.txt
Partial sketch and results
(1.91 KiB) Downloaded 2 times

User avatar
EBCrp
 
Posts: 16
Joined: Fri Nov 11, 2022 6:19 pm

Re: String from a JSON data line

Post by EBCrp »

Resolved!! In case someone else has the question this is what finally resolved the issue:

String conditions = api_object["weather"][0]["main"];

Note the [0] between "weather" and "main". Now I just need to find out what it means!! :-)

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: String from a JSON data line

Post by dastels »

It means that api_object["weather"] results in a json array [0] pulls out the first element which is json dictionary. It is clearer if the json is prettyprinted:

Code: Select all

{
  "coord": {
    "lon": -82.9988,
    "lat": 39.9612
  },
  "weather": [
    {
      "id": 803,
      "main": "Clouds",
      "description": "broken clouds",
      "icon": "04d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 29.08,
    "feels_like": 22.05,
    "temp_min": 26.62,
    "temp_max": 31.8,
    "pressure": 1024,
    "humidity": 85
  },
  "visibility": 10000,
  "wind": {
    "speed": 6.91,
    "deg": 110
  },
  "clouds": {
    "all": 75
  },
  "dt": 1673185633,
  "sys": {
    "type": 2,
    "id": 2079804,
    "country": "US",
    "sunrise": 1673182418,
    "sunset": 1673216591
  },
  "timezone": -18000,
  "id": 4509177,
  "name": "Columbus",
  "cod": 200
}
Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”