MySQL Shell 8.4  /  ...  /  JSON 包裝

5.7.5 JSON 包裝

為了協助將 MySQL Shell 與外部工具整合,您可以使用 --json 選項,在從命令列啟動 MySQL Shell 時,控制所有 MySQL Shell 輸出的 JSON 包裝。 --json 選項僅對指定的 MySQL Shell 會話生效。

指定 --json--json=pretty--json=raw 會為會話開啟 JSON 包裝。 使用 --json=pretty 或未指定值的情況下,會產生美化列印的 JSON。 使用 --json=raw 時,會產生原始 JSON。

當 JSON 包裝開啟時,任何在組態檔或命令列 (使用 --result-format 選項或其別名之一) 上為 resultFormat MySQL Shell 組態選項指定的值都會被忽略。

指定 --json=off 會為會話關閉 JSON 包裝。 當 JSON 包裝關閉,或會話未要求時,結果集會以 resultFormat MySQL Shell 組態選項指定的格式正常輸出。

範例 5.7 使用美化列印 JSON 包裝的 MySQL Shell 輸出 (--json--json=pretty)

$> echo "select * from world_x.city where countrycode='AUT'" | mysqlsh --json --sql --uri user@localhost:33060
or
$> echo "select * from world_x.city where countrycode='AUT'" | mysqlsh --json=pretty --sql --uri user@localhost:33060
{
    "hasData": true,
    "rows": [
        {
            "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
            }
        }
    ],
    "executionTime": "0.0067 sec",
    "affectedRowCount": 0,
    "affectedItemsCount": 0,
    "warningCount": 0,
    "warningsCount": 0,
    "warnings": [],
    "info": "",
    "autoIncrementValue": 0
}

範例 5.8 使用原始 JSON 包裝的 MySQL Shell 輸出 (--json=raw)

$> echo "select * from world_x.city where countrycode='AUT'" | mysqlsh --json=raw --sql --uri user@localhost:33060
{"hasData":true,"rows":[{"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}}],"executionTime":"0.0117 sec","affectedRowCount":0,"affectedItemsCount":0,"warningCount":0,"warningsCount":0,"warnings":[],"info":"","autoIncrementValue":0}