Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda