[SOLVED] How can I solve FireBase database forbidden characters in Key or a workaround

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.


I have this FireBase database structure an really would love to have the key like this example:

"US": {
    "name": "United States",
        "US/California/Orange County/Orange/3138 E Maple Ave": "-K1234567"
        "US/California/Orange County/Orange/3139 E Maple Ave": "-K12334rt"
        "US/California/Orange County/Orange/3140 E Maple Ave": "-K1234r34"

Because the key is unique it´s a really good idea if it wasn’t for the Firebase forbidden database characters:

. (period)    
$ (dollar sign)    
[ (left square bracket)    
] (right square bracket)    
 (hash or pound sign)    
/ (forward slash)    

The California path that the above key consist of comes from Google Places api, and I wonder if any more of the forbidden database characters can pop up, and how to counteract this from blowing up when a future key have a # or $.

I also need a way to work with this key like:

String[] split = theKey.split("/");

If I replace the (forward slash) what should I use, or how can a do this in another way maybe. Does Google Places api´s autocomplete result have any of the forbidden database characters?

I could do like:

// test for . $ # [ ] / and replace them
public static String removeInvalidChars(String text) {
    text = text.replace(".", "");
    text = text.replace("$", "");
    text = text.replace("#", "");
    text = text.replace("[", "");
    text = text.replace("]", "");
    text = text.replace("/", "");
    return text;

With this removeInvalidChars it will blow up I think, feels like I loose control over the key structure:

"US/California/Orange County/Orange/3138 E Maple Ave"

Maybe should I create s separate root node where a hash version of the above path is stored. Then I save the Hashes like :

"US": {
    "name": "United States",
        "sadsasasjkinIGhGVBJKHBHIUOuijHdBJLI": "US/California/Orange County/Orange/3138 E Maple Ave",
        "KJHGjgh67HGCFhgvhjbIuiokkBHJbkjhBkj": "US/California/Orange County/Orange/3139 E Maple Ave",
        "LKJHNJHKHky7y78gHGHGJ675JHJHdbHJBjh":  "US/California/Orange County/Orange/3139 E Maple Ave"

Hey maybe that´s the answer…mm what do you think?


Most developers store encoded values in the keys. The most common one is when storing email addresses, in which case they escape the . in the email address with a ,.

"puf@firebaseui,com": true

In your case I see two main options:

  1. Encoded the offending characters in the key.
  2. Filter the offending characters from the key.

Filtering is definitely simpler, but is non-reversible: you can never get the exact value back from the key in that case.

Encoding is reversible. For example: we could get the email address back from the previous example by converting the , back into a .. Since a , can never occur in an email address, the conversion is lossless and reversible

Answered By – Frank van Puffelen

people found this article helpful. What about you?