How To display JSON data in a Laravel blade template

To display JSON data in a Laravel blade template, you can use the json_decode function to parse the JSON string into a PHP array or object. You can then loop through the array or access the object properties to display the data.

For example, if you have the following JSON string:

{
  "name": "John",
  "age": 30,
  "city": "New York"
}

You can parse it into a PHP object in your controller and pass it to the view like this:

$json = '{"name": "John","age": 30,"city": "New York"}';
$data = json_decode($json);

return view('view', ['data' => $data]);

Then, in your blade template, you can access the object properties like this:

Name: {{ $data->name }}<br>
Age: {{ $data->age }}<br>
City: {{ $data->city }}

Or, if you prefer to loop through the object properties, you can use a foreach loop:

@foreach ($data as $key => $value)
    {{ $key }}: {{ $value }}<br>
@endforeach

If the JSON data is an array, you can loop through it using a foreach loop as well:

@foreach ($data as $item)
    Name: {{ $item->name }}<br>
    Age: {{ $item->age }}<br>
    City: {{ $item->city }}<br>
@endforeach

You can also use the @json directive to output the JSON data as a string in your blade template:



@json($data)

This will display the JSON data as a string, with appropriate encoding for special characters.

 

 

 

 

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now