0

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.

1
  • entities[0].key.name Commented Dec 16, 2015 at 7:01

5 Answers 5

1

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

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

Comments

1

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

Comments

1

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

1 Comment

@user2924127 happy to help.
1

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

Comments

1

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

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.