I have the following JSON object:
"StudentData": {
"Name": "Mike",
"Age": 25,
"DateOfBirth": 9/25/1993,
"IsMarried": false
}
I'm working on a Javascript function that will present a dialog box to a user with the above information inside of it, however I'd like the information to be preceded with the object's name (i.e. "StudentData").
Let's say I have this object stored within a variable in my function, and let's call it myStudent
.
I stumbled upon a post on SE using Object.keys(myStudent)[0]
to get property name of the first key
within StudentData
, but I'm looking to extract the name of the object itself.
How may I go about doing this?
Thank you :)
EDIT:
I'm looking to get StudentData
string, not any of the keys contained within the StudentData
object itself.
I have the following JSON object:
"StudentData": {
"Name": "Mike",
"Age": 25,
"DateOfBirth": 9/25/1993,
"IsMarried": false
}
I'm working on a Javascript function that will present a dialog box to a user with the above information inside of it, however I'd like the information to be preceded with the object's name (i.e. "StudentData").
Let's say I have this object stored within a variable in my function, and let's call it myStudent
.
I stumbled upon a post on SE using Object.keys(myStudent)[0]
to get property name of the first key
within StudentData
, but I'm looking to extract the name of the object itself.
How may I go about doing this?
Thank you :)
EDIT:
I'm looking to get StudentData
string, not any of the keys contained within the StudentData
object itself.
-
You simply use a dot like so:
obj.StudentData.Name
. This is of course assuming as Kevin B says below, that you've usedJSON.parse
and that it's valid json (the stuff you posted is not valid json - you're missing starting brackets andDateOfBirth
isn't a string). – h2ooooooo Commented Mar 1, 2018 at 16:42 -
That isn't an object, and
StudentData
once the json is converted to an object isn't in any way related to the object that it contains. – Kevin B Commented Mar 1, 2018 at 16:42 - how the output will look like? – brk Commented Mar 1, 2018 at 16:44
- How is that data being stored? The "object" you are talking about does not really have a "name." It is likely just the value in an object associated with a key of "StudentData". Objects do not have "names" but they can be stored in other objects as values associated with keys. – Helam Commented Mar 1, 2018 at 16:45
-
What do you mean by "name of the object"? Are you trying to get the string
myStudent
? Can't do that. Are you looking to get the name of an object's keys? Looks like you already know how. – Dave Newton Commented Mar 1, 2018 at 17:00
3 Answers
Reset to default 5but I'm looking to extract the name of the object itself.
Objects do not themselves have "names". There is some confusion in other answers thinking you are just trying to get the "Name" property inside the object, and not trying to get the value "StudentData". The only way you could do this is if it was stored in an object something like this:
let myObject = {
"StudentData": {
"Age": 25,
"IsMarried": false
}
}
And then you could use the solution you found of Object.keys(myObject)[0]
to get "StudentData"
but if it is not stored like that there is no standard way of getting that value. Even in the above example, the object containing Age
etc does not have a "name" of "StudentData", it is simply associated with the key "StudentData" in the outer object.
Try like this:
var obj = {
"StudentData": {
"Name": "Mike",
"Age": 25,
"DateOfBirth": 9/25/1993,
"IsMarried": false
}
};
// to get the first key
var keys = Object.keys(obj);
console.log(keys[0]);
// or to get the StudentData keys:
var objectKeys = Object.keys(obj.StudentData);
console.log(objectKeys);
// or to populate dinamically a table
let k, tr = '';
for (k in obj.StudentData) {
tr += '<tr><td>' + k + '</td><td>' + obj.StudentData[k] + '<td></tr>';
}
$('table.popup tbody').append(tr);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="popup">
<thead>
<tr>
<td><b>Property</b></td>
<td><b>Value</b></td>
</tr>
</thead>
<tbody></tbody>
</table>
If you stored this object as
var myStudent = { "StudentData": {
"Name": "Mike",
"Age": 25,
"IsMarried": false
}
}
Just do simply myStudent.StudentData.Name
to get the value 'Mike'.
And If you really wants to get the key out of object. you can run the below code.
( function getKeyValueFromJSON() {
var myStudent = { "StudentData": {
"Name": "Mike",
"Age": 25,
"IsMarried": false
}
}
for(var val in myStudent.StudentData) {
console.log("Key: " + val + " value: " + myStudent.StudentData[val]);
}
})();