Delfino
Delfino

Reputation: 1009

Get JSON Object Name - Javascript

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.

Upvotes: 1

Views: 18674

Answers (3)

shanwije
shanwije

Reputation: 610

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]);
    }
 })();

Upvotes: 0

Helam
Helam

Reputation: 1505

but 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.

Upvotes: 5

Claudio
Claudio

Reputation: 5213

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.com/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>

Upvotes: 2

Related Questions