NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
https://www.php.net/manual/en/function.json-decode.php
So you don't have a valid json in that file.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I can't use my json file on @foreach, because json_decode returns null. Look:
My perguntas.json file:
{
"questions": [
{ "ck": ["c1", "c6"], "id": 1, "text": "Você se sente \"desligado\" ou \"aéreo\"?" },
{ "ck": ["c1", "c2"], "id": 2, "text": "Tem tendências adictas compulsivas " },
{ "ck": ["c1"], "id": 3, "text": "Sente-se abatido e com pouca energia?" },
{ "ck": ["c1"], "id": 4, "text": "Sente-se apreensivo ou ansioso sem motivo aparente?" },
]
}
My Controller:
$perguntas = file_get_contents(resource_path('lang/pt-br/perguntas.json'));
$getperguntas = json_decode($perguntas);
This does not work, as I get NULL. If I print on the screen without doing the decode, my json looks like this: https://prnt.sc/uuw51r
Why doesn't it work and how do I solve it?
You have a comma after your last } within questions. In PHP it is allowed but in JSON it is not.
This is the same json without the comma, and this is valid:
{
"questions": [
{
"ck": [
"c1",
"c6"
],
"id": 1,
"text": "Você se sente \"desligado\" ou \"aéreo\"?"
},
{
"ck": [
"c1",
"c2"
],
"id": 2,
"text": "Tem tendências adictas compulsivas "
},
{
"ck": [
"c1"
],
"id": 3,
"text": "Sente-se abatido e com pouca energia?"
},
{
"ck": [
"c1"
],
"id": 4,
"text": "Sente-se apreensivo ou ansioso sem motivo aparente?"
}
]
}
Please or to participate in this conversation.