Skip to content

Instantly share code, notes, and snippets.

@spnkr
Created July 7, 2018 19:39
Show Gist options
  • Save spnkr/35df7024606d232b752db0842e06a51b to your computer and use it in GitHub Desktop.
Save spnkr/35df7024606d232b752db0842e06a51b to your computer and use it in GitHub Desktop.
Find invalid JSON keys for Firebase import
#sometimes when you try to import a json file to firebase via the console, firebase says "invalid keys" (If you create your own keys, they must be UTF-8 encoded, can be a maximum of 768 bytes, and cannot contain ., $, #, [, ], /, or ASCII control characters 0-31 or 127.) This script will print out the invalid keys so you can manually fix them.
#useful if you have a giant firebase json file not generated from another db.
#Caveat: quick and dirty, only goes a few levels deep. theoretically you could use recursion to go n-levels deep, but I didn't need this for my use case.
#Caveat: doesn't check for utf-8 encoding, or ASCII control characters 0-31 and 127.
require 'json'
#put keys with ., $, #, [, ], /, or > 768 bytesize here. I used 768-100 for the bytesize for caution. probably should be 768-1.
bad_keys = []
#put your firebase json export file path here:
file = File.open("path to the json file from firebase.json", "rb")
# the script:
invalid = file.read
json = JSON.parse(invalid)
allk = []
allk << json.keys
json.keys.each{|k|
if json[k].respond_to?(:keys)
json[k].keys.each do |ik|
allk << ik
if json[k][ik].respond_to?(:keys)
json[k][ik].keys.each do |ik2|
allk << ik2
if json[k][ik][ik2].respond_to?(:keys)
json[k][ik][ik2].keys.each do |ik3|
allk << ik3
if json[k][ik][ik2][ik3].respond_to?(:keys)
json[k][ik][ik2][ik3].keys.each do |ik4|
allk << ik4
end
end
end
end
end
end
end
end
}
allk2 = allk.flatten.flatten.compact.uniq
allk2.each do |k|
if !k.index('.').nil? || !k.index('$').nil? || !k.index('#').nil? || !k.index('[').nil? || !k.index(']').nil? || !k.index('/').nil? || (k.bytesize > (768-100))
bad_keys << k
end
end
#all done
puts "bad keys are:"
puts bad_keys
```
@spnkr
Copy link
Author

spnkr commented Jul 7, 2018

Sometimes when you try to import a json file to firebase via the console, firebase says "invalid keys".

If you create your own keys, they must be UTF-8 encoded, can be a maximum of 768 bytes, and cannot contain ., $, #, [, ], /, or ASCII control characters 0-31 or 127.) This script will print out the invalid keys so you can manually fix them.

  • useful if you have a giant firebase json file not generated from another db.

  • Caveat quick and dirty, only goes a few levels deep. theoretically you could use recursion to go n-levels deep, but I didn't need this for my use case.

  • Caveat doesn't check for utf-8 encoding, or ASCII control characters 0-31 and 127.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment