Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!