user2924127
user2924127

Reputation: 6242

Access Json array

I have the following JSON I get sent back from my database and I am trying to access the 'name' and 'contact's keys:

[
  {
    "key": {
      "name": "a",
      "kind": "Users",
      "path": [
        "Users",
        "a"
      ]
    },
    "data": {
      "fullname": "a",
      "password": "a",
      "contacts": [
        "bob", "john"
      ]
    }
  },
  {
    "key": {
      "name": "b",
      "kind": "Users",
      "path": [
        "Users",
        "b"
      ]
    },
    "data": {
      "fullname": "b",
      "password": "b",
      "contacts": [
        "john"
      ]
    }
  }
]

My java script callback function looks something like this and inside I am trying to print the values from the entities variable:

dataset.get(keys, function(err, entities) {});

I tried: entities.key.name[0] to get the first name key value, but it does not work. I am also stuck how to then get the contacts variable.

Upvotes: 0

Views: 60

Answers (5)

Rajesh
Rajesh

Reputation: 24915

You can try something like this:

var json = [{
  "key": {
    "name": "a",
    "kind": "Users",
    "path": [
      "Users",
      "a"
    ]
  },
  "data": {
    "fullname": "a",
    "password": "a",
    "contacts": [
      "bob", "john"
    ]
  }
}, {
  "key": {
    "name": "b",
    "kind": "Users",
    "path": [
      "Users",
      "b"
    ]
  },
  "data": {
    "fullname": "b",
    "password": "b",
    "contacts": [
      "john"
    ]
  }
}]

function getJSONValues(){
  var names = [], contacts = [];
  for (var obj in json){
    console.log(json[obj]);
    
    names.push(json[obj].key.name);
    contacts.push(json[obj].data.contacts.splice(0));
  }
  
  console.log(names, contacts)
}

getJSONValues();

Upvotes: 1

void
void

Reputation: 36703

Use entities[ 0 ][ "key" ].name; as entities is an array

Upvotes: 1

Christos
Christos

Reputation: 53958

You have an array. So you have to use an index to read an item from it.

entities[0]

returns the first object.

{
"key": {
  "name": "a",
  "kind": "Users",
  "path": [
    "Users",
    "a"
  ]
},
"data": {
  "fullname": "a",
  "password": "a",
  "contacts": [
    "bob", "john"
  ]
}
}

The you can use either the dot notation or brackets notation to read the values of your object. For instance, if you want to read the key, just try this:

entities[0].key

Now key has a reference to the following object:

{
  "name": "a",
  "kind": "Users",
  "path": [
    "Users",
    "a"
  ]
}

So, if you want to get the name, just do one more step:

entities[0].key.name

The same holds for the contacts:

entities[0].data.contacts

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68393

if the variable to which this JSON object is assigned to is entities

then

entities[ 0 ][ "key" ].name; //will give you name
entities[ 0 ][ "data" ].contacts; //will give you contacts

now you can iterate the entities as

entities[ counter ][ "key" ].name; //will give you name
entities[ counter ][ "data" ].contacts; //will give you contacts

Upvotes: 1

mplungjan
mplungjan

Reputation: 177786

Use console to see what is what:

var entities = [
  {
    "key": {
      "name": "a",
      "kind": "Users",
      "path": [
        "Users",
        "a"
      ]
    },
    "data": {
      "fullname": "a",
      "password": "a",
      "contacts": [
        "bob", "john"
      ]
    }
  },
  {
    "key": {
      "name": "b",
      "kind": "Users",
      "path": [
        "Users",
        "b"
      ]
    },
    "data": {
      "fullname": "b",
      "password": "b",
      "contacts": [
        "john"
      ]
    }
  }
];
console.log(entities);
console.log(entities[1].key.name);
console.log(entities[1].data.contacts[0]);

Upvotes: 1

Related Questions