2

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.

5
  • You simply use a dot like so: obj.StudentData.Name. This is of course assuming as Kevin B says below, that you've used JSON.parse and that it's valid json (the stuff you posted is not valid json - you're missing starting brackets and DateOfBirth isn't a string). 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. Commented Mar 1, 2018 at 16:42
  • how the output will look like? 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. 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. Commented Mar 1, 2018 at 17:00

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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>

Comments

0

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

2 Comments

They're looking for a way to get the key name, not data from a key.
9/25/1993 is not a valid number - this would never work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.