0

I have the following JSON data structure. I would like to know how could I extract the data providing the keys.

"Meta Data": {
    "1. Information": "High School",
    "2. Name": "St Marys"
},
"Teachers' Names": {
    "Grade I": {
        "1. English": "Amanda Fernandez",
        "2. Sociology": "Christina Andrew",
        "3. Applied Science": "George Binu"
    },
    "Grade II": {
        "1. English": "Gedfd Hkdfd",
        "2. Sociology": "lksdg klsdfd",
        "3. Applied Science": "some one else"
    }
  }
}

I use nodejs and get this data in a 'body' object under request module. How do I extract the Grade I, English Teacher name above? I tried body["Teachers' Names][0] to see if it would give me at least the Grade I object in full. But it didn't. Can someone please help me?

7
  • Is the body always in the same structure or is it generated dynamically? Commented Dec 16, 2017 at 0:27
  • The structure is the same but the data is getting generated dynamically based on what I m querying the API for. Commented Dec 16, 2017 at 1:52
  • Let's say you always have "Meta Data" and "Teachers Names" and only the childs of these change... You could use Object.keys or an "for..in" loop to go trough the properties. If you want one specific value you shouldn't loop trough all the properties of the object. Commented Dec 16, 2017 at 1:57
  • Yes I always would have Meta Data and Teachers Names and the data inside these change. Can you show me example of how to use Object.keys to pick only the first Teachers name object values? You can consider the above data set as an example if you would like. I am only interested in fetching 1. English from first Teachers names object. Appreciate all your help. Thanks Commented Dec 16, 2017 at 2:06
  • If you'll always pick the first property you're better of using "for..in" since this is an loop which you can break so you'll only read the first property. See this jsfiddle for an example: jsfiddle.net/esrm9bc0 Commented Dec 16, 2017 at 2:17

3 Answers 3

1
var grade1 = body["Teachers' Names"]["Grade I"];

Since you are accessing an object, not an array, you should use the property names rather than integer index.

In response to your comment, if you want to get the first grade simply by specifying the int 0 rather than the string "Grade I" you can do the following:

var teachersNames = body["Teachers' Names"];
var first = teachersNames[Object.keys(teachersNames)[0]];

Here you are using the function Object.keys to get and array of the property names and then getting the zeroth item in that array and then using that item as the key into the object teachersNames.

if you then want the "1. English" value you can do:

var result = first["1. English"];

This is pretty gruesome and hints that you should consider structuring your Json differently to better meet your needs. Such as usitng arrays rather than objects.

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

2 Comments

This works. Thank you. In my business logic, I just want to take the ["1. English"] value from [Teachers' Names] object without specifying the Grade I. In other words, I would like to just look at the first Teachers' Names object and extract the subject teachers names? Is there a way to do that?
@SamT see my modified answer.
0

There are multiple ways to achieve the same goal (as always). But to answer your question here we go with one way to accomplish your goal.

const body = {
  "Meta Data": {
    "1. Information": "High School",
    "2. Name": "St Marys"
  },
  "Teachers' Names": {
    "Grade I": {
      "1. English": "Amanda Fernandez",
      "2. Sociology": "Christina Andrew",
      "3. Applied Science": "George Binu"
    },
    "Grade II": {
      "1. English": "Gedfd Hkdfd",
      "2. Sociology": "lksdg klsdfd",
      "3. Applied Science": "some one else"
    }
  }
}

// to access known keys you could do it like this
console.log(data['Teachers\' Names']['Grade I']['1. English']) // this will output "Amanda Fernandez"

Comments

0

You can either refer to keys as @bhspencer mentions above, using "s if the key contains ', and vice versa. You can also escape a quote in the string with \, or use es6 template literals if they are available.

var str = `"something" 'here'`;
var body = {};
body[str] = 1;

var grade1 = body['"something" \'here\'']; // works
var grade1 = body[`"something" 'here'`]; // if you have es6 template literals

Some people like these other routes if things get too complex and they want consistency, or to pass style guide tests.

2 Comments

I am not sure I understand your justification for using a single quote string and escape the apostrophe. Could you say more why this helps with consistency?
@bhspencer this isn't a good example of that scenario--I'll edit for that point, thanks

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.