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

84
Visualizações
JSON parse double quotes

I encountered a very strange error. If I run JSON.Parse('[""d""]') then I have error: "Unexpected token d in JSON at position 3"

JSON.parse('["\"d\""]')

If I run console.log(JSON.stringify(['"d"'])) then I get '[""d""]'

console.log(JSON.stringify(['"d"']))

If I run console.log(JSON.parse(JSON.stringify(['"d"']))) then I get good result [""d""]

console.log(JSON.parse(JSON.stringify(['"d"'])))

If I use eval function then I have error: "Uncaught SyntaxError: Unexpected token d in JSON at position 3"

var someJson=JSON.stringify(['"d"']);
window.eval(`JSON.parse('${someJson}')`)

How fix it?

I use some external library and there is window.eval(JSON.parse('${JSON.stringify(t)}'))). Where t - json array with strings. String can contains double quoutes. It throws exception.

I found js changed my string value when I set it.

var someJson='["\"d\""]';
console.log(someJson);

It returns '[""d""]' and when I run JSON.Parse it is incorrect JSON.

How fix it?

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

0

JSON.parse('["\"d\""]')

\ is a special character in JS strings, so to include it in your JSON you need to escape it.

const yourString = '["\"d\""]';
const validJSON = '["\\"d\\""]'
const parsedData = JSON.parse(validJSON);

document.querySelector('#yourString').value = yourString;
document.querySelector('#validJSON').value = validJSON;
<p>Your string: <output id="yourString"></output>
<p>Valid JSON: <output id="validJSON"></output>

As a rule of thumb, embedding JSON in string literals is a waste of time and effort. You can just write the JS data structure you get from parsing the JSON.

about 4 years ago · Juan Pablo Isaza Relatório

0

@Quentin many thanks for idea! Result answer for me:

var someJson=JSON.stringify(['"d"']);
console.log(window.eval(`JSON.parse('${someJson.replaceAll('\\','\\\\')}')`))

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