Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kramsuiluj's avatar

Error when displaying JSON.parse() data on console.

I'm returning a json to my blade view then I'm passing it to javascript. But I'm getting an error: Uncaught SyntaxError: expected property name, got '&'

I don't know how a parsed json should look like, when I click on the error view source it looked like this

const jsonData = JSON.parse({"1":{"id":23434,"firstname":"Kerri","middlename":"Ellis","lastname":"Chelsey","email":"[email protected]","email_verified_at":null,"profile_type":"App\Models\TeacherProfile","profile_id":14,"created_at":"2021-10-22T16:28:00.000000Z","updated_at":"2021-10-22T16:28:00.000000Z","profile":{"id":14,"contact":"09295554077","created_at":"2021-10-22T16:28:00.000000Z","updated_at":"2021-10-22T16:28:00.000000Z"}}

I'm guessing I need to escape the &? but I'm not really sure and I don't know how.

This is my js code I was trying to check if I was getting the data.

<script>
    const jsonData = JSON.parse({{ $teachers }});
    console.log(jsonData);
</script>
0 likes
4 replies
kramsuiluj's avatar

@MichalOravec Oh I see, sorry, I had a process in mind that I was trying out, retrieve the collection > convert to array > to json > decode in the view > pass to js, I was trying to do it step by step so its something that I can surely in my level. Turns out you can do it with 2 steps. Thanks a lot again!

Please or to participate in this conversation.