Laravel: How do I parse this json data in view blade?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel: How to Parse JSON Data in View Blade
Introduction: One of the most useful features of modern web applications is the ability to display data from different resources. In your Laravel application, you might receive a JSON object with important information that needs to be displayed in the view. This blog post will show you how to parse the given JSON data in your Laravel view using Blade templating engine and provide some best practices for handling data in PHP.
1. Prepare the View: Before we begin, create a new resource controller and related views with their corresponding routes. We will use the "Leads" as an example of our JSON object that needs to be displayed. In your controller, define a method to handle the index route as follows:
public function index() {
$data = [
'leads' => json_decode(file_get_contents('example-json.txt'), true),
'title' => 'All Leads'
];
return view('leads.index', $data);
}
2. Parse the JSON Data in Blade: In your "leads.index.blade.php" view, access this `$leads` variable that contains the JSON object and parse it to an associative array using the PHP function json_decode(). Then, loop through each member and display meaningful information about them:
@foreach($leads as $lead)
Member ID: {{ $lead['id'] }}
Firstname: {{ $lead['firstName'] }}
Lastname: {{ $lead['lastName'] }}
Phone: {{ $lead['phoneNumber'] }}
Owner ID: {{ $ownerId = '10' }} (Assuming owner data is stored in another JSON file)
Firstname: {{ $owner['firstName'] = 'First' }}
Lastname: {{ $owner['lastName'] = 'Last' }}
@endforeach
3. Use View Composers for Better Organization and Performance: Instead of passing the entire JSON data as a variable, create a custom view composer that will handle this data and provide it to your views automatically when needed. This will improve the organization and performance of your application. Create a new composer class named "LeadComposer" in your controllers folder and register it using `View::composer()`.
public function compose(View $view) {
$data = json_decode(file_get_contents('example-json.txt'), true);
$view->with('leads', $data['member']);
$leadOwnerId = '10';
$ownerData = json_decode(file_get_contents('owners/'.$leadOwnerId.'.json'), true);
$view->with('owner', $ownerData);
}
4. Conclusion: Parsing JSON data in Laravel views using the Blade templating engine can be done with a few lines of code and proper planning. By following best practices, you can improve your application's organization and performance while ensuring that your code is clean and maintainable. For more information on this topic, check out https://laravelcompany.com/blog/parsing-json-data-in-laravel-views for an in-depth guide on handling data in Laravel applications.