search
LoginSignup
0

posted at

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

無効な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

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
What you can do with signing up
0