MySQL Shell 8.0  /  ...  /  JSON 形式の出力

このページは機械翻訳したものです。

5.7.4 JSON 形式の出力

MySQL Shell には、結果セットを出力するためのいくつかの JSON 形式オプションが用意されています:

json または json/pretty

これらのオプションは、どちらも整形出力 JSON を生成します。

ndjson または json/raw

これらのオプションはどちらも改行で区切られた RAW JSON を生成します。

json/array

このオプションは、JSON 配列にラップされた RAW JSON を生成します。

これらの出力形式を選択するには、MySQL Shell を --result-format=value コマンドラインオプションで起動するか、MySQL Shell 構成オプション resultFormat を設定します。

バッチモードでは、MySQL Shell を外部ツールと統合するために、--json オプションを使用して、コマンドラインから MySQL Shell を起動するときのすべての出力の JSON ラッピングを制御できます。 JSON ラッピングがオンの場合、MySQL Shell は整形出力 JSON (デフォルト) または RAW JSON を生成し、resultFormat MySQL Shell 構成オプションの値は無視されます。 その手順は、セクション5.7.5「JSON ラッピング」を参照してください。

例 5.4 整形出力 JSON 形式での出力 (json または json/pretty)

Press CTRL+C to copy
MySQL localhost:33060+ ssl world_x JS > shell.options.set('resultFormat','json') MySQL localhost:33060+ ssl world_x JS > session.sql("select * from city where countrycode='AUT'") { "ID": 1523, "Name": "Wien", "CountryCode": "AUT", "District": "Wien", "Info": { "Population": 1608144 } } { "ID": 1524, "Name": "Graz", "CountryCode": "AUT", "District": "Steiermark", "Info": { "Population": 240967 } } { "ID": 1525, "Name": "Linz", "CountryCode": "AUT", "District": "North Austria", "Info": { "Population": 188022 } } { "ID": 1526, "Name": "Salzburg", "CountryCode": "AUT", "District": "Salzburg", "Info": { "Population": 144247 } } { "ID": 1527, "Name": "Innsbruck", "CountryCode": "AUT", "District": "Tiroli", "Info": { "Population": 111752 } } { "ID": 1528, "Name": "Klagenfurt", "CountryCode": "AUT", "District": "Kärnten", "Info": { "Population": 91141 } } 6 rows in set (0.0031 sec)

例 5.5 改行デリミタを使用した RAW JSON 形式での出力 (ndjson または json/raw)

Press CTRL+C to copy
MySQL localhost:33060+ ssl world_x JS > shell.options.set('resultFormat','ndjson') MySQL localhost:33060+ ssl world_x JS > session.sql("select * from city where countrycode='AUT'") {"ID":1523,"Name":"Wien","CountryCode":"AUT","District":"Wien","Info":{"Population":1608144}} {"ID":1524,"Name":"Graz","CountryCode":"AUT","District":"Steiermark","Info":{"Population":240967}} {"ID":1525,"Name":"Linz","CountryCode":"AUT","District":"North Austria","Info":{"Population":188022}} {"ID":1526,"Name":"Salzburg","CountryCode":"AUT","District":"Salzburg","Info":{"Population":144247}} {"ID":1527,"Name":"Innsbruck","CountryCode":"AUT","District":"Tiroli","Info":{"Population":111752}} {"ID":1528,"Name":"Klagenfurt","CountryCode":"AUT","District":"Kärnten","Info":{"Population":91141}} 6 rows in set (0.0032 sec)

例 5.6 JSON 配列でラップされた RAW JSON 形式の出力 (json/array)

Press CTRL+C to copy
MySQL localhost:33060+ ssl world_x JS > shell.options.set('resultFormat','json/array') MySQL localhost:33060+ ssl world_x JS > session.sql("select * from city where countrycode='AUT'") [ {"ID":1523,"Name":"Wien","CountryCode":"AUT","District":"Wien","Info":{"Population":1608144}}, {"ID":1524,"Name":"Graz","CountryCode":"AUT","District":"Steiermark","Info":{"Population":240967}}, {"ID":1525,"Name":"Linz","CountryCode":"AUT","District":"North Austria","Info":{"Population":188022}}, {"ID":1526,"Name":"Salzburg","CountryCode":"AUT","District":"Salzburg","Info":{"Population":144247}}, {"ID":1527,"Name":"Innsbruck","CountryCode":"AUT","District":"Tiroli","Info":{"Population":111752}}, {"ID":1528,"Name":"Klagenfurt","CountryCode":"AUT","District":"Kärnten","Info":{"Population":91141}} ] 6 rows in set (0.0032 sec)