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

96
Views
Parsing a list of JSON elements in Javascript

What I'm trying to do: I'm trying to parse a list of json objects. I made a REST API that returns a list of JSON objects, formatted like this:

[{"_id":"61d7aca1ae700aeb4f9f9470","username":"test","email":"test@test.com","hasConfirmedEmail":false,"password":"test1234","createdAt":"2022-01-07T02:59:45.408Z","updatedAt":"2022-01-07T02:59:45.408Z","__v":0,"type":"user"}]

and I want to get the username of the first one. How would I do this in Javascript & JQuery?

What I'm doing: I've tried doing:

jQuery.ajax({
    url: 'localhost:3000/my/api/route',
    type: 'GET',

    contentType: 'application/json; charset=utf-8',
    success: function(data) {
        // using err to visualize the data temporarily
        err.innerHTML = json.parse(data)[0].username
    },
    error: function(req, status, thrown) {
        err.innerHTML = thrown
    },

    timeout: 120000,
});

but that just returns: [object, Object]

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because the object is in an array indicated by the square brackets on each end you can access the first item by using the index 0, then get the username from the object.

data[0].username
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!