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

320
Visualizações
Converting a string to an *almost* JSON

So I have a string that looks like this:

'{utopia:1,word:2,sentence:3,tourism:4,home:5}'

I'd like to convert it to an object that looks like:

{utopia:1,word:2,sentence:3,tourism:4,home:5}

I tried using JSON.parse() on the original string but got 'SyntaxError: Unexpected token u in JSON at position 1'. I think this is because the keys of the object don't have quotations around them (in a classic JSON object, it would be like "utopia":1,...)

Not sure how to fix this. Any suggestions are much appreciated.

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

0

I think you can first replace { and } in string with empty strin then yourstring.split(',') After that [items].reduce((a, v) => ({ ...a, [v]: v}), {}) to build your object

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