Noman Ali Noman Ali - 3 years ago 195
Javascript Question

Parsing firebase object javascript

It might be duplicate, sorry for that but I couldn't understand (New to firebase and JS).

I have a snapshot from firebase, but I couldn't understand how to parse that snapshot object.

Here is my code:

var obj = snapshot.val();
console.log(JSON.stringify(obj));


It outputs following:

{"-KpxDFnJEt2xlD21lzyh":{"chatid":"6qKi8xO5vxdrcKFd5wqUNUkTupg2PNQjC87cFNcKxYkDoYMdhH95LCK2"}}


I want to get this
-KpxDFnJEt2xlD21lzyh
value in a separate variable and
chatid
into separate variable, but not be able to.

What should I do?

Answer Source

To get the key name and its chatid value:

var json = {
  "-KpxDFnJEt2xlD21lzyh": {
    "chatid": "6qKi8xO5vxdrcKFd5wqUNUkTupg2PNQjC87cFNcKxYkDoYMdhH95LCK2"
  }
}
for (key in json) {
  if (!json.hasOwnProperty(key)) continue;
  console.log(key);
  console.log(json[key].chatid);
}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download