0

I have a javascript function which returns the response like this( I am using nodejs and ejs):

"index_1": {
    "mappings": {
        "type_1": {
            "properties": {
                "field_1": {
                    "type": "string"
                },
                "field_2": {
                    "type": "string"
                }
            }
        },
        "type_2": {
            "properties": {
                "field_1": {
                    "type": "string"
                },
                "field_2": {
                    "type": "string"
                },
                "field_3": {
                    "type": "string"
                }
            }
        }
    }
}

Now, I need to access 2nd or third level key from the response. Suppose if I want a list like this:

type_1
type_2

or

field_1
field_2
field_3

How can I do that? If I use callback(Object.keys(response)) then it returns index_1. Can anyone point me to right direction?

1
  • Do you need a particular field or type, or would you like to perform something for each of them? Commented Jan 20, 2017 at 15:43

2 Answers 2

1

To get the keys of a sub-object, you need to pass this particular sub-object to Object.keys():

var data = {"index_1":{"mappings":{"type_1":{"properties":{"field_1":{"type":"string"},"field_2":{"type":"string"}}},"type_2":{"properties":{"field_1":{"type":"string"},"field_2":{"type":"string"},"field_3":{"type":"string"}}}}}};

console.log(Object.keys(data.index_1.mappings));                   
    // ["type_1", "type_2"]

console.log(Object.keys(data.index_1.mappings.type_2.properties)); 
    // ["field_1", "field_2", "field_3"]

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

Comments

0

There is no simple one-liner, I suppose.

Object.keys( object );

returns only first level keys (that's the reason you get index_1).

Solution 1

If you know, that response, has alway a structure of:

var jsonObject = {
   "index_1" : { 
       "mappings": {
            "type1" : ... ,
            "type2" : ...
       }
};

Then you only need to pass:

callback(Object.keys(jsonObject.index1.mappings));

That way you'll get third level keys.

But if you don't know the structure, or want to access keys of any level, then recursion might be helpful.

var jsonObject = {
   "index_1" : {
       "mappings": {
            "type1" : { field1 : {}, field2 : 2} ,
            "type2" : {}
       }
   }
};

// 1..N, and 1 means you want to get **index_1**
function getNthLevelKeys( json, level ) {
      var keys = [];
      var currLvlKeys = Object.keys(json);
      level = level - 1;

      if ( typeof json !== 'object' || json === null) {
          return [];
      }

      if ( level > 0 ) {

           for (var i = 0; i < currLvlKeys.length; i++) {
               keys = keys.concat(getNthLevelKeys( json[ currLvlKeys[i] ] , level ));
            }
      }

      if (level === 0) {
            return currLvlKeys;
      }

      if (level < 0) {
           throw new Error("Cannot access level "+level+" of this object");
      }

      return keys;
}

console.log(getNthLevelKeys( jsonObject , 1));
console.log(getNthLevelKeys( jsonObject , 2));
console.log(getNthLevelKeys( jsonObject , 3));
console.log(getNthLevelKeys( jsonObject , 4));

Comments

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.