-2

I have a JSON object like below

{
 "db": {
 "name": "db",
 "connector": "memory"
  },
"MySql": {
   "host": "localhost",
   "port": 3306,
   "database": "users",
   "username": "root",
   "password": "",
   "name": "MySql",
   "connector": "mysql"
  },
"postgreDS": {
   "host": "localhost", 
   "port": 1234,
   "database": "abc", 
   "username": "postgres", 
   "password": "abc",
   "name": "postgreDS",
   "connector": "postgresql"
 }

I need the following output from this object.

{"db", "MySql", "postgreDS"}

how should I extract the above information from whole JSON object in JavaScript. and how I will read the sub keys and there values.

Rizwan Haider
  • 167
  • 2
  • 17
  • Iterate over it, get all three dictionaries, extract the values for key `name` from all three dictionaries. unless you are looking for ready-made code – NSNoob Nov 26 '15 at 13:44
  • 1
    Possible duplicate of [Getting JavaScript object key list](http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list) – Sachin Nov 26 '15 at 13:44
  • keys can be unknown to me. – Rizwan Haider Nov 26 '15 at 13:45
  • So what? You can get all keys using a `for-in` loop. As long as your target is to get all values against the key `name` from all child dicts. Or you could use simple `var keys = Object.keys(dictionary)` to get all the keys – NSNoob Nov 26 '15 at 13:46
  • 1
    @RizwanHaider Make one thing clear here! Do you want to display the top level keys db, mysql and postGreDS? Or do you want to display value for the key `name` inside all three of them? – NSNoob Nov 26 '15 at 13:52
  • Possible duplicate of [Get Property Names In JSON Objects](http://stackoverflow.com/questions/1876485/get-property-names-in-json-objects) – Jude Niroshan Nov 26 '15 at 13:56
  • @NSNoob Thanks :) I got your point. I want to just diplay the top lever keys for now.. for an other scenario I have to read the keys for these top lever keys. I think to do that same logic will be used... am I right? – Rizwan Haider Nov 26 '15 at 19:48

3 Answers3

1

Something like this (where obj is your JSON-object):

var objectProperties = [];
for(var prop in obj){
    objectProperties.push(prop);
}
console.log(JSON.stringify(objectProperties)); // ["db", "MySql", "postgreDS"]
Arg0n
  • 8,283
  • 2
  • 21
  • 38
1

If you're merely looking to get the keys of the nested objects use Object.keys:

var keys = Object.keys(obj); // [ "db", "MySql", "postgreDS" ]

If you're looking to get the values from within the nested objects based on a specified key here's a generic function that will return an array of values:

function getValueFromKey(obj, key) {
    return Object.keys(obj).map(function (el) {
        return obj[el][key];
    });
}

getValueFromKey(obj, 'name'); // [ "db", "MySql", "postgreDS" ]
getValueFromKey(obj, 'connector'); // [ "memory", "mysql", "postgresql" ]

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0

Try the FIDDLE,

javascript

$(function(){
var output = {};
var items = {
 "db": {
 "name": "db",
 "connector": "memory"
  },
"MySql": {
   "host": "localhost",
   "port": 3306,
   "database": "users",
   "username": "root",
   "password": "",
   "name": "MySql",
   "connector": "mysql"
  },
"postgreDS": {
   "host": "localhost", 
   "port": 1234,
   "database": "abc", 
   "username": "postgres", 
   "password": "abc",
   "name": "postgreDS",
   "connector": "postgresql"
 }};

for(var propertyName in items) {
    output.push(propertyName); // output will have the required output

}
console.log(output);
});
Mayank
  • 1,351
  • 5
  • 23
  • 42