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

190
Visualizações
convert textarea input to javascript object?

bit of a junior dev here, i have a text area which will receive an input of an array of objects like so ..

[
  {
     utc: "xxxxxx",
     battery_level: 12,
     lat: 2345678,
     lng: 234567,
   },
   {
     utc: "xxxxxx",
     battery_level: 12,
     lat: 2345678,
     lng: 234567,
    }
]

the text area input converts it to string like..

'[\n  {\n     utc: "xxxxxx",\n     battery_level: 12,\n… 12,\n     lat: 2345678,\n     lng: 234567,\n    }\n]'

i need to convert that back to a javascript object. i have tried JSON.parse which throw an error 'unexpected token u at position 10' the u of utc because it is NOT a string key.

i have tried JSON.stringify to JSON.parse which still returns a string.

the project is using react with typescript. any help would be appreciated.

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

0

The problem is that the JavaScript objects may or may not have quotes, while the JSON standard requires double quotes for each property name.

For this reason, you cannot use JSON.parse, since that is not a valid JSON.

You have three alternatives:

  • Create a parser that converts your string into an object (zero dep.)
  • Use eval, which would introduce vulnerabilities. You said that users can paste anything into the textbox, so the use of eval is highly discouraged
  • Use JSON5

Indeed, JSON5 does exactly what you need to do:

JSON5.parse('[\n  {\n     utc: "xxxxxx",\n     battery_level: 12,\n… 12,\n     lat: 2345678,\n     lng: 234567,\n    }\n]')
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