Get data from JSON file with PHP

Get data from JSON file with PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding Nested JSON Structures in PHP

When you need to extract specific values like temperatureMin and temperatureMax from a JSON file, the process is straightforward once you understand how PHP handles unstructured text data. The core challenge isn't fetching the file; it's navigating the nested object structure to reach the exact keys you need.

Start by reading the raw content of your file.json into a string variable using file_get_contents(). This function returns the file as a plain string, which PHP cannot interpret on its own. To make the data usable, you must decode it into an associative array. This is where json_decode() comes into play.

$rawData = file_get_contents('file.json');
$data = json_decode($rawData);

The crucial detail here is that json_decode() returns an object by default, not an array. While both work for simple access, working with objects allows you to use dot notation (e.g., $data->daily) which often feels more intuitive when mapping back to the JSON keys. If you prefer array syntax like $data['daily'], you would need to pass true as the second argument to json_decode(). Stick with the default object return type unless you have a specific reason to switch, as it preserves the hierarchy of your data source more clearly.

Once decoded, you can access the nested properties directly. The path to your target fields is $data->daily->data[0]->temperatureMin. This looks cumbersome quickly, so it helps to break down the navigation step by step. First, access the daily object. Inside that, find the data key, which holds an array of daily records. Since the example shows a single entry in that array, you access the first element using index 0. Finally, you pull the specific temperature keys from that record.

$minTemp = $data->daily->data[0]->temperatureMin;
$maxTemp = $data->daily->data[0]->temperatureMax;

However, relying on hardcoded indices like [0] is a fragile approach. If your JSON file ever contains more than one day of data, or if the order changes, your code might grab the wrong record. It is best practice to check how many items exist in the data array before accessing it. This prevents errors when the dataset is empty or larger than expected.

$dailyRecord = $data->daily->data[0];

// Optional safety check for arrays with multiple entries
if ($data->daily && count($data->daily->data) > 0) {
    foreach ($data->daily->data as $day) {
        // Process each day individually here
    }
}

Another common pitfall involves type safety. json_decode() returns null if the JSON is malformed or if the file path is incorrect. Always verify that $data exists before trying to access its properties. Attempting to read $data->daily when $data is null will throw a fatal error in PHP, crashing your script immediately.

if ($data === null) {
    // Handle parsing error or missing file gracefully
}

If you are building this logic within a Laravel application, the pattern remains similar but benefits from the framework's service container and type hints. You can pass the decoded data to a model or controller method that expects an object. For more complex applications, consider using a DTO (Data Transfer Object) or a dedicated API client library to handle the JSON parsing and normalization automatically, reducing boilerplate code in your controllers.

If you need further assistance integrating these patterns into a larger Laravel project, resources like https://laravelcompany.com can provide guidance on best practices for handling data structures within the ecosystem.

Our network