Viewed   76 times

I'm trying to get data from the following JSON file using PHP. I specifically want "temperatureMin" and "temperatureMax".

It's probably really simple, but I have no idea how to do this. I'm stuck on what to do after file_get_contents("file.json"). Some help would be greatly appreciated!

    "daily": {
        "summary": "No precipitation for the week; temperatures rising to 6° on Tuesday.",
        "icon": "clear-day",
        "data": [
                "time": 1383458400,
                "summary": "Mostly cloudy throughout the day.",
                "icon": "partly-cloudy-day",
                "sunriseTime": 1383491266,
                "sunsetTime": 1383523844,
                "temperatureMin": -3.46,
                "temperatureMinTime": 1383544800,
                "temperatureMax": -1.12,
                "temperatureMaxTime": 1383458400,



Get the content of the JSON file using file_get_contents():

$str = file_get_contents('');

Now decode the JSON using json_decode():

$json = json_decode($str, true); // decode the JSON into an associative array

You have an associative array containing all the information. To figure out how to access the values you need, you can do the following:

echo '<pre>' . print_r($json, true) . '</pre>';

This will print out the contents of the array in a nice readable format. Note that the second parameter is set to true in order to let print_r() know that the output should be returned (rather than just printed to screen). Then, you access the elements you want, like so:

$temperatureMin = $json['daily']['data'][0]['temperatureMin'];
$temperatureMax = $json['daily']['data'][0]['temperatureMax'];

Or loop through the array however you wish:

foreach ($json['daily']['data'] as $field => $value) {
    // Use $field and $value here


Monday, October 17, 2022

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022

The problem is that you have JSON inside JSON.

you have to decode twice:

$string = file_get_contents("products.json");
$json_a = json_decode($string, true); //here you turn a JSON-string into an array containing JSON-strings

$arr = array();
foreach ($json_a as $key) {
    array_push($arr,json_decode($key[0],true)); //and here you turn each of those JSON-strings into objects themselves


echo json_encode($arr);

gives me this:

    "id": 1474541876849,
    "name": "D",
    "price": "12"
}, {
    "id": 1474541880521,
    "name": "DD",
    "price": "12"
}, {
    "id": 1474541897705,
    "name": "DDDGG",
    "price": "124"
}, {
    "id": 1474541901141,
    "name": "FAF",
    "price": "124"
}, {
    "id": 1474543958238,
    "name": "tset",
    "price": "6"

which is valid JSON itself and probably what you want.

Thursday, December 22, 2022

Your code creates new dictionary object for each object with:


Moreover, it overwrites the previous contents of the variable. Old dictionary in m_dict is deleted from memory.

Try to create a list before your for loop and store the result there.

result = []
for item in json_decode:

Finally, write the result to the output:


Printing the dictionary object aims to help the developer by showing the type of the data. In u'Diego Velxe1zquez', u at the start indicates a Unicode object (string). When object using is printed, it is decoded according to current language settings in your OS.

Sunday, September 25, 2022

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
    echo "true";    
else {
    echo "false";   
Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :