Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json data format in firebase - are arrays supported? And/Or, if only objects are supported, can dictionaries be numbered with integers?

Tags:

json

firebase

I am tinkering with firebase and curious about the data structure. Browsing to my database, firebase allows me to modify the structure and data in my database. But it seems that firebase only supports objects (and dictionaries for lists).

I want to know if arrays are supported. I would also like to know if dictionary items can be named with integers - the firebase interface only inserts strings as names which makes me concerned about ordering records.

Here is a sample of json created through firebase interface:

{
 "dg":{
  "users":{
   "rein":{
    "searches":{
     "0":{
      "urls":"http://reinpetersen.com,http://www.reinpetersen.com",
      "keyphrases":"rein  petersen,reinsbrain,programmer turned kitesurfer"
     }
    }
   },
   "jacqui":{
    "searches":{
     "0":{
      "urls":"http://www.diving-fiji.com,http://diving-fiji.com",
      "keyphrases":"diving marine conservation, diving fiji"
     }
    }
   }
  },
  "crawl_list":{
   "1":{
    "urls":"http://www.diving-fiji.com,http://diving-fiji.com",
    "keyphrases":"diving marine conservation, diving fiji"
   },
   "0":{
    "urls":"http://reinpetersen.com,http://www.reinpetersen.com",
    "keyphrases":"rein  petersen,reinsbrain,programmer turned kitesurfer"
   }
  }
 }
}

Obviously, for my lists, I want the dictionary item names to be integers so i can ensure sorting is correct.

like image 627
Reinsbrain Avatar asked Jul 07 '12 17:07

Reinsbrain


2 Answers

You can save arrays into Firebase. For example:

var data = new Firebase(...);
data.set(['x', 'y', 'z']);

Javascript Arrays are essentially just objects with numeric keys. When retrieving data, we automatically detect when a Firebase object has only numeric keys, and we return an array if that is the case.

Note that for storing a list of data to which many people can append, an array is not a good choice, as multiple people writing to the same index in the array can cause conflicts. Instead, we have a "push" function which creates a chronologically-ordered unique ID for your data.

Also, if you're intending to use the array as a way of ordering data, there's a better way to do that using our priorities. See the docs.

like image 102
Andrew Lee Avatar answered Oct 21 '22 06:10

Andrew Lee


The Firebase docs have a pretty good section on how to order your data: Ordered Data.

Just like JSON fields, Firebase fields can only be named with strings. It sounds like what you're looking for is setWithPriority(), which attaches sortable priority data to your fields, or push(), which is guaranteed to give your fields unique names, ordered chronologically. (More on lists and push() here.)

You can also push() or set() arrays. For example,

new Firebase("http://gamma.firebase.com/MyUser").push(["cakes","bulldozers"]);

results in a tree like you'd expect, with MyUser receiving a uniquely named child who has children "0":"cakes" and "1":"bulldozers".

like image 24
bennlich Avatar answered Oct 21 '22 06:10

bennlich