Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

438
Visualizações
Unicode JSON in Laravel blade for use in React, generating a JSON.parse problem

I have a json object in DB:

{
"ui": {},
"title": "Hola mundo 2",
"values": {},
"properties": {},
"description": "descripcion"
}

In the Laravel controller, I send the data to the view:

$surveyData = Empresa::find($empresa_id)->survey;    
return view('surveys.edit')->with('surveyData', json_encode($surveyData));

In the Laravel Blade, I attach the data in a div:

<div id="poll" data-survey=@json($surveyData)></div>

In React I get the data and save in oldData variable:

const oldData = document.querySelector("#poll").dataset.survey

The problem is that I'm getting a JSON.parse error in React because that JSON print in the Laravel view DOM in the following unicode format:

<div id="poll" data-survey="\u0022{\\\u0022ui\\\u0022: {}, \\\u0022title\\\u0022: \\\u0022Hola mundo 2\\\u0022, \\\u0022values\\\u0022: {}, \\\u0022properties\\\u0022: {}, \\\u0022description\\\u0022: \\\u0022descripcion\\\u0022}\u0022"></div>

when I parse that json in React:

JSON.parse(oldData)

I'm getting this error:

Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I really don't know where is the error or what else to do.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You seem to double encode your JSON data: json_encode($surveyData) and then @json($surveyData) Change

return view('surveys.edit')->with('surveyData', json_encode($surveyData));

to

return view('surveys.edit')->with('surveyData', $surveyData);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda