You have a json object that contains a field with key employees and value list of employees, to get the first employee you should access the list using the employees key, after accessing the value you can call the first item 0
You should access employees first
In the code, you have a JSON string assigned to the employees variable, which you then parse into a JavaScript object. The JSON represents an object with a property employees which is an array of employee records.
If the JSON is correctly parsed into the object myobj, and you want to access the firstName of the second employee (JavaScript is zero-indexed, so the second employee is at index 1), your code would look like this:
console.log(myobj.employees[1].firstName);
Make sure you access the employees array within the parsed object. If you attempt to access myobj[1], it will be undefined because myobj is not an array but an object with an employees property that is an array.
If you are receiving an error, please check the following:
Ensure the JSON string is valid.
Ensure you’re accessing the correct property and index.
If these are correct, the console.log() should output "Mary", which is the firstName of the second object in the employees array.
2
Answers
You have a json object that contains a field with key
employees
and value list of employees, to get the first employee you should access the list using theemployees
key, after accessing the value you can call the first item0
You should access employees first
In the code, you have a JSON string assigned to the employees variable, which you then parse into a JavaScript object. The JSON represents an object with a property employees which is an array of employee records.
If the JSON is correctly parsed into the object myobj, and you want to access the firstName of the second employee (JavaScript is zero-indexed, so the second employee is at index 1), your code would look like this:
Make sure you access the employees array within the parsed object. If you attempt to access myobj[1], it will be undefined because myobj is not an array but an object with an employees property that is an array.
If you are receiving an error, please check the following:
If these are correct, the console.log() should output "Mary", which is the firstName of the second object in the employees array.