How to load json file into Laravel Controller?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Load JSON File into a Laravel Controller: A Developer's Guide

When building applications with Laravel, developers frequently need to integrate external data sources. One common scenario is loading configuration data, lists of IDs, or complex settings stored in simple file formats like JSON. Having a static JSON file, such as /storage/app/calendar_Ids.json, is a straightforward way to store relational data that doesn't require the overhead of a full database transaction for small datasets.

As a senior developer, I can tell you that while reading a file seems simple, handling it robustly—especially concerning error management and performance—is crucial in a production environment. This guide will walk you through the most correct and practical ways to load your JSON data into a Laravel Controller.

The Recommended Approach: Using the Storage Facade

The best practice in Laravel is to leverage the built-in Storage facade to interact with files stored on the disk (like the storage directory). This keeps your code decoupled from raw PHP file operations and ensures you are using Laravel's provided abstraction layers.

For this example, we will assume the file /storage/app/calendar_Ids.json exists and contains a valid JSON array of IDs.

Step 1: Accessing the File Content

Instead of manually using file_get_contents() on an absolute path, we use the Storage facade to retrieve the content. This method is cleaner and aligns better with Laravel's architecture for file management.

In your Controller method, you would utilize the Illuminate\Support\Facades\Storage class.

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log; // For error logging

class CalendarController extends Controller
{
    public function loadCalendarIds()
    {
        $filePath = 'calendar_Ids.json'; // File name relative to the 'storage' disk

        // Attempt to retrieve the file content using the Storage facade
        if (!Storage::disk('local')->exists($filePath)) {
            Log::error("JSON file not found at path: " . $filePath);
            return response()->json(['error' => 'Data file not found'], 404);
        }

        $jsonContent = Storage::disk('local')->get($filePath);

        // Step 2: Decode the JSON content
        $calendarIds = json_decode($jsonContent, true); // Decode as associative array (true)

        // Step 3: Handle potential decoding errors
        if (json_last_error() !== JSON_ERROR_NONE) {
            Log::error("JSON decoding error: " . json_last_error_msg());
            return response()->json(['error' => 'Error parsing JSON data'], 500);
        }

        // If successful, return the data
        return response()->json([
            'status' => 'success',
            'ids' => $calendarIds
        ]);
    }
}

Best Practices and Error Handling

The code above demonstrates several critical best practices:

  1. Use Facades: Rely on Storage::disk('local')->get() instead of raw PHP functions. This is essential when building maintainable applications, following the principles outlined by Laravel documentation regarding service usage.
  2. Check Existence First: Always check if the file exists using Storage::exists(). If a file is missing, attempting to read it will throw an error, leading to poor user experience and potential security issues. We log this failure for debugging purposes.
  3. Validate JSON Decoding: The most common point of failure when dealing with external data is malformed JSON. We use json_last_error() after decoding to explicitly catch any parsing errors.

When to Consider Alternatives (The Database Path)

While loading simple configuration files via JSON is quick for prototyping, it’s important to recognize its limitations in a large-scale system. If the data you are loading frequently changes, requires complex querying, or needs transactional integrity, storing this data directly in a relational database (using Eloquent models) is significantly superior.

Laravel is designed to handle persistence elegantly. When dealing with structured data, utilizing migrations and Eloquent relationships provides better indexing, security, and scalability than managing raw JSON files on the filesystem. As you grow your application, focus on leveraging Laravel’s powerful ORM capabilities rather than relying solely on file storage for core business data.

Conclusion

Loading a JSON file into a Laravel Controller is achievable by combining file system interaction with PHP's json_decode function. However, the key to writing professional code lies in robust error handling and utilizing Laravel's provided facades like Storage. By implementing checks for file existence and JSON parsing errors, you ensure your application remains stable, secure, and maintainable. Remember, always look for the most idiomatic and scalable solution provided by the framework when dealing with persistence!