Parse JSON encoded data in Vue Component
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Parsing JSON Encoded Data in Vue Components: A Developer's Guide
As developers building modern Single Page Applications (SPAs) using frameworks like Vue, React, or Angular, handling data flow between the backend and the frontend is a daily task. One common scenario involves passing structured data, often serialized as JSON, from a server-side rendering (SSR) framework like Laravel into a Vue component as a prop.
I’ve seen this exact issue repeatedly: you successfully pass the data, but when attempting to access nested properties within your Vue component, the data appears unreadable or undefined. Let's dive into why this happens and how to correctly parse JSON data when working with Vue components.
The Problem: Strings vs. Objects in Data Transfer
The core issue stems from how data is transferred across the boundary between PHP (Laravel) and JavaScript (Vue).
In your provided example, you are using json_encode($userDetails) in Blade to output a string into an HTML attribute:
<my-profile user-details="{{ json_encode($userDetails) }}">
When Vue receives this data via the props, it generally treats the entire value as a string. Although that string looks like valid JSON, JavaScript doesn't automatically convert it into a usable object upon receipt. Therefore, when you try to access properties like userDetails.first_name in your component, you are accessing properties on a plain string, which results in errors or undefined values.
The output you observed confirms this: the component receives the raw JSON string, not the parsed data structure.
Solution 1: Parsing the JSON in the Vue Component (Direct Fix)
The most straightforward solution is to explicitly parse the received string into a JavaScript object immediately upon receiving the prop within your Vue component's setup or script.
Here is how you can modify your MyProfile.vue component to handle this correctly:
<template>
<div class="container">
<div class="row justify-content">
<div class="col-md-3" id="profile-image">
<img class="img-fluid rounded" src="https://www.w3schools.com/bootstrap4/paris.jpg" alt="Profile Image">
</div>
<div class="col-md-12">
<!-- Access the parsed data here -->
<p>{{ parsedUserDetails.first_name }}</p>
<p>Name: {{ parsedUserDetails.first_name }}</p>
</div>
</div>
</div>
</template>
<script>
export default {
props: ["userDetails"],
computed: {
// Use a computed property to parse the prop immediately
parsedUserDetails() {
if (this.userDetails) {
try {
return JSON.parse(this.userDetails);
} catch (e) {
console.error("Error parsing JSON:", e);
return null;
}
}
return null;
}
}
}
</script>
By using a computed property, we ensure that the parsing happens only when the prop is available, making the component robust. This pattern ensures that the data structure is correctly established before any template rendering occurs.
Solution 2: The Best Practice – API-Driven Data Fetching
While parsing the string works for simple static data passed via SSR, relying on embedding large JSON strings directly into HTML attributes is generally not scalable or secure for dynamic applications. A more robust and professional approach involves treating your backend as a true API source.
Instead of passing the entire encoded object as a prop, you should use Laravel to serve structured JSON data via an API endpoint. Your Vue component then fetches this data asynchronously using methods like axios. This separation keeps your frontend decoupled from the specific serialization details of the backend, aligning with modern architectural principles often explored in projects utilizing frameworks like those found on https://laravelcompany.com.
Backend Setup (Laravel Example)
Ensure your controller returns JSON:
// In your Laravel Controller method
return response()->json($userDetails); // Laravel handles the json_encode() automatically here
Frontend Setup (Vue/Axios Example)
The Vue component fetches the data, eliminating the need for complex string parsing on initial load:
import axios from 'axios';
export default {
props: [], // No props needed initially
data() {
return {
userDetails: null
};
},
mounted() {
this.fetchUserDetails();
},
methods: {
async fetchUserDetails() {
try {
// Assuming your backend provides an endpoint
const response = await axios.get('/api/user-details');
this.userDetails = response.data; // Data is automatically parsed by Axios
} catch (error) {
console.error("Failed to fetch user details:", error);
}
}
}
}
Conclusion
When passing complex data between Laravel and Vue, understand the context of the data transfer. For small, static payloads embedded in HTML attributes, manually parsing the JSON string using JSON.parse() within a Vue computed property is an effective fix. However, for dynamic applications, leveraging RESTful APIs—where the backend (like Laravel) handles serialization and the frontend (Vue) handles asynchronous fetching—is the superior, scalable, and maintainable pattern. Always aim for API communication when handling dynamic data flow throughout your application architecture.