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

73
Visualizações
Javascript parse a string with commas

I am getting this string from my api call:

"{'attachments': [{'type': 'html', 'html': 'text-test'}, {'type': 'album', 'album': [{'url': 'htps://s3.eu-central-8f7ba8d118dbf1/pic/cdyegkwkaije7720e2f72.jpg', 'type': 'image'}]}]}"

I need to find a way to parse it and be able to iterate over the items in attachments list, but when I try to run JSON.parse(str) I am getting an error, I guess because the word attachments has commas around it.

The error is Uncaught SyntaxError: Unexpected token ' in JSON at position 1 at JSON.parse

How can I decode it to be able to reach the attachments?

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

0

you have to replace ' with " . try this

var jsonStr ="{'attachments': [{'type': ..." //your api string

jsonStr = jsonStr.replaceAll("'","\"");
var result = JSON.parse(jsonStr);
about 4 years ago · Juan Pablo Isaza Relatório

0

JSON requires ", not '. If this is your own API, you should change the implementation to return " quotations. Best to use JSON.stringify(objectDataToSend) if your API is written in JavaScript.

If this is not your own API, I'd suggest using the String.replaceAll() function to replace all ' with ":

let data = "{'attachments': [{'type': 'html', 'html': 'text-test'}, {'type': 'album', 'album': [{'url': 'htps://s3.eu-central-8f7ba8d118dbf1/pic/cdyegkwkaije7720e2f72.jpg', 'type': 'image'}]}]}"

const result = JSON.parse(data.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