LoginSignup
0
0

More than 1 year has passed since last update.

Invalid JSON Keys cannot be empty or contain $#[]./【Firebase Realtime Database】

Posted at

無効なJSON キーを空にすることはできません。また、キーに $#[]./ を含めることはできませんと表示されたため備忘録
image.png

$#[]./をエンコードすることで保存できた

JavaScript

const encodeKey = s => s.replace(/[\.\$\[\]#\/%]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
const decodeKey = s => s.replace(/%(2E|24|5B|5D|23|2F|25)/g, decodeURIComponent)

const obj = {'.$[]#/': 'hoge'}
saveAsJSON(Object.fromEntries(Object.entries(obj).map(([k, v]) => [encodeKey(k), v])))

Python

import re
import urllib.parse

encode_key = lambda s: re.sub('[\.\$\[\]#\/%]', lambda c: f'%{ord(c.group()):X}', s)
decode_key = lambda s: re.sub('%(2E|24|5B|5D|23|2F|25)', lambda c: urllib.parse.unquote(c.group()), s)
print(encode_key('.$[]#/%23'))

See the Pen Invalid JSON on Firebase by John Doe (@04) on CodePen.

参考

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0