永井 忠一 2024.10.10
JSON 形式との対応
| JavaScript | Perl | Python | PostgreSQL | SQLite | |
|---|---|---|---|---|---|
| object | |
|
|
|
|
| array | |
|
|
|
|
| string | |
|
|
|
|
| number | |
|
|
|
|
| true | |
|
|
|
|
| false | |
|
|
|
|
| null | |
|
|
|
|
結果
| JavaScript | Perl | Python | SQL | |
|---|---|---|---|---|
| object | object | HASH | dict | |
| array | ARRAY | list | ||
| string | string | SCALAR | str | text |
| number | number | int | INTEGER | |
| float | REAL | |||
| true | boolean | bool | BOOLEAN | |
| false | ||||
| null | object | NoneType |
(確認に使用したソフトウェア、バージョン)
$ deno Deno 1.44.1 exit using ctrl+d, ctrl+c, or close() REPL is running with all permissions allowed. To specify permissions, run `deno repl` with allow flags. >
$ firefox --version Mozilla Firefox 129.0.2
$ perl --version This is perl 5, version 38, subversion 2 (v5.38.2) built for x86_64-linux-gnu-thread-multi (with 44 registered patches, see perl -V for more detail) Copyright 1987-2023, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at https://www.perl.org/, the Perl Home Page.
$ ipython3 Python 3.12.3 (main, Sep 11 2024, 14:17:37) [GCC 13.2.0] Type 'copyright', 'credits' or 'license' for more information IPython 8.20.0 -- An enhanced Interactive Python. Type '?' for help. In [1]:
$ pypy3 Python 3.9.18 (7.3.15+dfsg-1build3, Apr 01 2024, 03:12:48) [PyPy 7.3.15 with GCC 13.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>>
$ psql psql (16.4 (Ubuntu 16.4-0ubuntu0.24.04.2)) Type "help" for help. =>
$ sqlite3 SQLite version 3.45.1 2024-01-30 16:01:20 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database. sqlite>
| JavaScript | Perl | Python | PostgreSQL | SQLite | |
|---|---|---|---|---|---|
| object | |
|
|
|
|
| array | |
|
|
|
|
| string | |
|
|
|
|
| number | |
|
|
|
|
| true | |
|
|
|
(※SQLite では、BOOLEAN 型は INTEGER 型) |
| false | |
|
|
|
|
| null | |
|
|
|
|
(Perl の JSON モジュールの関数名)
$ perl -MJSON::PP -le 'print from_json(q("abc"))' JSON::PP::from_json has been renamed to decode_json. at -e line 1.
$ perl -MJSON::PP -le 'print to_json("abc")' JSON::PP::to_json has been renamed to encode_json. at -e line 1.
(PostgreSQL の「 #>>」演算子を使った例は、CAST() 関数による text 型への型変換として書くこともできる)
RFC 8259 では、許可されていない
Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. ...
各メソッド、関数の実行結果
| JavaScript | Perl | Python | PostgreSQL | SQLite |
|---|---|---|---|---|
|
|
|
|
|
(Perl の JSON::XS モジュールでは、NaN が「-nan」とエンコードされている)
(Python の json.dumps() メソッドで「allow_nan=False」オプションを指定した場合には、ValueError となる)
ValueError: Out of range float values are not JSON compliant: nan
ValueError: Out of range float values are not JSON compliant: inf
ValueError: Out of range float values are not JSON compliant: -inf
(SQLite では、NaN(となる演算の結果)が NULL になる)
各メソッド、関数のパース結果
| JavaScript | Perl | Python | PostgreSQL | SQLite |
|---|---|---|---|---|
|
|
|
|
|
(Python では受け入れられる)
(SQLite では、NaN は NULL となる。また、json_valid() 関数が提供されている)
日付 時刻型は、JSON では定義されていない
各メソッド、関数の実行結果
| JavaScript | Perl | Python | PostgreSQL | SQLite |
|---|---|---|---|---|
|
|
|
|
|
(Perl の DateTime は、以下のようになる)
$ perl -MJSON -MDateTime::Format::ISO8601 -le 'print encode_json(DateTime::Format::ISO8601->parse_datetime("1970-01-01T00:00:00.000Z"));' encountered object '1970-01-01T00:00:00', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing) at -e line 1.
(Python でも、オプション「default=str」を指定しないと TypeError になり、文字列には変換されない)
TypeError: Object of type datetime is not JSON serializable
《Perl と Python の日付 時刻について》
JSON として、パースできるかテスト
| PostgreSQL | ||||
|---|---|---|---|---|
|
|
|
|
|
(重複するキーの確認)
| PostgreSQL |
|---|
|
(と、キーの数)
| PostgreSQL |
|---|
|
(JavaScript の Object、Perl のハッシュ、Python の dict(辞書)のキー)
| JavaScript | Perl | Python |
|---|---|---|
|
|
|
json_each() 関数
| PostgreSQL |
|---|
|
(JavaScript、Perl 、Python での、同様の処理)「key、value」の組に展開
| JavaScript | Perl | Python |
|---|---|---|
|
|
|
json_insert()、json_replace()、json_set()、json_remove() 関数
| SQLite |
|---|
|
json_array_length() 関数
| PostgreSQL | SQLite |
|---|---|
|
|
json_array_elements()、json_array_elements_text() 関数
| PostgreSQL |
|---|
|
(違いは、json 型で取得するか、text 型で取得するか)
json_to_record()、json_to_recordset() 関数
| PostgreSQL |
|---|
|
(逆変換)ドットコマンド「.mode json」を利用
| SQLite |
|---|
|
(SQLite の、json モードのヘルプ)
sqlite> .help mode .mode MODE ?OPTIONS? Set output mode MODE is one of: ...snip... json Results in a JSON array ...snip...
(パス指定)json_extract_path()、json_extract() 関数と、JSON パス式
| PostgreSQL | SQLite |
|---|---|
|
|
text 型として取り出す
| PostgreSQL | SQLite |
|---|---|
|
|
(PostgreSQL では、json_extract_path_text() 関数が用意される)
json_patch() 関数
| SQLite |
|---|
|
(詳細は RFC 7396)
(PosgreSQL と SQLite ともに、JSON のバイナリ表現がある)
© 2024 Tadakazu Nagai