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

234
Visualizações
How to print a specific JSON field?

I start from a JSON string, try to convert it into a JSON object and then I try to print one specific field (for example firstName), but I obtain undefined. What am I doing wrong? Thank you!

var string = '{"firstName":"John", "lastName":"Doe"}'
var obj = JSON.stringify(string)
var json_object = JSON.parse(obj)
console.log(json_object.firstName)
console.log(json_object['firstName'])
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The string is string. So you don't need to stringify that.

var string = '{"firstName":"John", "lastName":"Doe"}'
var json_object = JSON.parse(string)
console.log(json_object.firstName)

about 4 years ago · Juan Pablo Isaza Relatório

0

var string = '{"firstName":"John", "lastName":"Doe"}';
var obj = JSON.parse(string);
console.log(obj.firstName)

The object is a string, you only need to parse the JSON and print what you want. If you had created an JavaScript object, there you can use Stringify for stringifying, but it wouldn't be necessary. JSON Parse parses a String in JSON.

about 4 years ago · Juan Pablo Isaza Relatório

0

When you pass a string to JSON.stringify, that is equivalent to pass a string object to JSON.stringify.

After you execute the code below, you actually got the result '"{\\"firstName\\":\\"John\\", \\"lastName\\":\\"Doe\\"}"' of the variable obj.

var string = '{"firstName":"John", "lastName":"Doe"}'
var obj = JSON.stringify(string)

At the end, use JSON.parse to parse '"{\\"firstName\\":\\"John\\", \\"lastName\\":\\"Doe\\"}"' string, we will get the string object of '{"firstName":"John", "lastName":"Doe"}'.

For detail, you can check Autoboxing: primitive wrapper objects in JavaScript.

So, you can just remove JSON.stringify(string), and pass '{"firstName":"John", "lastName":"Doe"}' to JSON.parse directly.

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