5

I am trying to get the keys of the nested nodes in Firebase and I am not sure how to do this.

For example in this case:

example

How do I know that 2,3,4 exist within 1?

I am thinking of putting a values in a list seperately in firebase. But is there a smarter way of doing this? Is there a more efficient way of getting the keys of all the nested nodes in Firebase?

ndduong
  • 429
  • 9
  • 21
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Jun 03 '16 at 12:15
  • oops sorry, im new to stackoverflow. Will do next time. Thanks for the feedback! – ndduong Jun 03 '16 at 13:16

3 Answers3

9

In Android

Gives access to all of the immediate children of this snapshot. Can be used in native for loops:

for (DataSnapshot child : parent.getChildren()) { 
   //Here you can access the child.getKey()
}

In iOS

Swift 3:

for (child in snapshot.children) { 
  //Here you can access child.key
}

Swift 4:

snapshot.children.forEach({ (child) in
  <#code#>
})

In Web

snapshot.forEach(function(childSnapshot) {
   //Here you can access  childSnapshot.key
});

You can put it in a different list or in the same path, the important thing is to keep in mind how much data you are really retrieving when calling an event. And how are you going to query that information... that is why it is recommended in NoSQL to keep flat nodes

Jason Shultz
  • 940
  • 1
  • 19
  • 36
Ymmanuel
  • 2,523
  • 13
  • 12
0

You need to working with Arrays of Dictionaries. Iterate through the each of the Dictionary then you need to check whether the key is present OR not. Because in Firebase We need to send specific keys to server.

SWIFT

let key = "1"
    for child in snapshot.children {
        if let snap = child.childSnapshotForPath(key){
            print("Able retrieve value : \(snap) for key : \(key)")
        } else {
            print("Unable to retrieve value for key : \(key)")
        }
    }

OBJECTIVE-C

NSString *key = @"1";
for (NSDictionary *dic in array) {
    NSArray *keys = [dic allKeys];
    if ([keys containsObject:key]){

    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
krunal
  • 452
  • 3
  • 11
  • The question is tagged "swift": please write your code examples in Swift. Thank you. – Eric Aya Jun 03 '16 at 09:04
  • 2
    Thank you for editing. // `The question also tagged as iOS` Yes, and? iOS itself doesn't mean Objective-C, it just means iOS. To code *for* iOS, there's Objective-C and also Swift now, and Ruby, and JavaScript... iOS is just a platform, it's not related to one specific language, and certainly not to Objective-C by default. – Eric Aya Jun 06 '16 at 08:04
0

I found a way to iterate over nested json whose key is random.

var ref = FIRDatabase.database().reference(withPath: "timinig")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        print(snapshot.childrenCount) // I got the expected number of items
        let enumerator = snapshot.children
        while let rest = enumerator.nextObject() as? FIRDataSnapshot {
            print("-")

            print(rest.key)
            let newobj=rest.children
            while let rest1 = newobj.nextObject() as? FIRDataSnapshot {
                print(rest1.key)
            }
            print("-")
        }
Rishabh Dugar
  • 606
  • 5
  • 16