PHP - how to get certain data out of the JSON?

  • 1
  • Question
  • Updated 12 months ago
I am trying to get the from the json response, but after hours of trying i do not get a response.
The example works: $temp_f = $parsed_json->{'current_observation'}->{'temp_f'};

My (partial) code:
$forecast_lows = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'period[2]'}->{'low'}->{'celsius'};

I tried many, many variations, like:
And dozens of other variations, based on stackoverflow feedback, internet searches, etc.

The code i found on the
$test = $data['forecast']['txt_forecast']['forecastday'][2]['fcttext'];

I know the data is here in the JSON (the -12 degrees C):

...etc etc etc

Mostly the PHP error i get is: Notice: Trying to get property of non-object
But many other errors i have seen too, like: Notice: Undefined property: stdClass::$forecastday[2]
i also tried to figure out the JSON structure with: echo '<pre>' . var_export($parsed_json, true) . '</pre>';
And i tried to  figure out the JSON with an online parser:

I just want the 10 celsius lows from the forecast10day.

Any help on how to traverse the complicated JSON is appreciated.
Photo of Marc de Ruijter

Marc de Ruijter

  • 3 Posts
  • 0 Reply Likes
  • confused

Posted 1 year ago

  • 1
Photo of James Anding

James Anding

  • 35 Posts
  • 10 Reply Likes
This how it should parse.

 var MyTest    = parsed_json['forecast']['simpleforecast']['forecastday'][2]['low']['celsius'];

It works for me.
Photo of Marc de Ruijter

Marc de Ruijter

  • 3 Posts
  • 0 Reply Likes
Hi James, 
Thanks for your reply!

$forecast_lows = $parsed_json['forecast']['simpleforecast']['forecastday'][2]['low']['celsius'];Gives: Fatal error: Uncaught Error: Cannot use object of type stdClass as array

$forecast_lows = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{2}->{'celsius'};
Gives: Notice: Trying to get property of non-object

This eror i get all the time.

In the examples {} brackets are used.
This is the PHP is use:
$json_string = file_get_contents("<my API key>/geolookup/forecast10day/q/IA/Cedar_Rapids.json"...;
$parsed_json = json_decode($json_string); # Decode the returned JSON string
Photo of Shawn


  • 1 Post
  • 1 Reply Like
Hi Marc! This is the deal,

$forecast_lows is the value

$parsed_json is the object

and what you are looking for is the key.

For an array, it will work this way: value =object->key->key->key...

You really have two options to code this:

$forecast_lows = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}{2}->{'low'}->{'celsius'};


$forecast_lows = $parsed_json->forecast->simpleforecast->forecastday[2]->low->celsius;

For testing to figure out what data is needed, you can always run a var dump:

This is new to me as well, if you ever want to chat/skype about this, let me know I'd love someone to bounce ideas off of.

Photo of Marc de Ruijter

Marc de Ruijter

  • 3 Posts
  • 0 Reply Likes
Hi Shawn, 

Thanks a bunch!
This part:  {'forecastday'}{2}  i had wrong all the time, i added an -> in between there also.
You saved the day.

I would like to have a sparring-partner too, i might have some valuable insights now-and-then.
Most of my contact info lives here atm: