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

263
Visualizações
How to convert string value to object in Javascript with command similar to eval in Python

In Python stringValue was given as a string variable:

stringValue = '{"DATA":{"VERSION":1.1, "STATE":True, "STATUS":"ONLINE"}}'

I can go ahead and "convert" or "cast" it as Python dictionary using eval built in function:

result = eval(stringValue)

Is there a way to do the same in javascript if we would be given var stringValue as:

var stringValue = '{"DATA":{"VERSION":1.1, "STATE":true, "STATUS":"ONLINE"}}'
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

use eval

With one caveat ... you'll need to add ( and ) to the string

// this is the ORIGINAL value of the string in the question!!!
var stringValue = "{'DATA':{'VERSION':1.1, 'STATE':true, 'STATUS':'ONLINE'}}";
var result = eval(`(${stringValue})`);
console.log(result)

Now that code in the question was changed by you after I posted the original answer ... i.e. swapped ' and " - now you CAN use JSON.parse as a comment suggested

var stringValue = '{"DATA":{"VERSION":1.1, "STATE":true, "STATUS":"ONLINE"}}';
var result = JSON.parse(stringValue);
console.log(result);

about 4 years ago · Santiago Gelvez 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