5.7.4 JSON格式输出

MySQL Shell提供了一些JSON格式选项来打印结果集:

jsonjson /漂亮

这两个选项都生成打印精美的JSON。

ndjsonjson /生

这两个选项都产生用换行符分隔的原始JSON。

json /数组

该选项生成封装在JSON数组中的原始JSON。

您可以通过使用——result-format =价值命令行选项,或者设置MySQL Shell配置选项resultFormat

在批处理模式下,为了帮助将MySQL Shell与外部工具集成,您可以使用——json选项控制JSON包装的所有输出,当你从命令行启动MySQL Shell。当JSON包装被打开时,MySQL Shell生成漂亮打印的JSON(默认值)或原始JSONresultFormatMySQL Shell配置选项被忽略。有关说明,请参见第5.7.5节,“JSON包装”

例5.4漂亮打印的JSON格式输出(jsonjson /漂亮

MySQL localhost:33060+ ssl world_x JS > shell.options.set('resultFormat','json')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": {"ID": 1526, "Name": "Salzburg", " countrycode ": "AUT", "District": "Salzburg", "Info": {"Population": 188022}} {"ID": 1526, "Name": " 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行集合(0.0031秒)

例5.5带换行分隔符的原始JSON格式输出(ndjsonjson /生

MySQL localhost:33060+ ssl world_x JS > shell.options.set('resultFormat','ndjson')sql(“select *从城市countrycode = AUT”){" ID ": 1523年,“名字”:“维也纳”,“countrycode”:“AUT”、“区”:“维也纳”,“信息”:{“人口”:1608144}}{" ID ": 1524年,“名字”:“格拉茨”,“countrycode”:“AUT”、“区”:“施泰尔马克”,“信息”:{“人口”:240967}}{" ID ": 1525年,“名字”:“林兹”,“countrycode”:“AUT”、“区”:“北奥地利”,“信息”:{“人口”:188022}}{" ID ": 1526年,“名字”:“萨尔斯堡”、“countrycode”:“AUT”、“区”:“萨尔斯堡”,“信息”:{“人口”:144247}}{"ID":1527,"Name":"Innsbruck","CountryCode":"AUT","District":"Tiroli","Info":{"Population":111752}} {"ID":1528,"Name":"Klagenfurt","CountryCode":"AUT","District":"Kärnten","Info":{"Population":91141}}

例5.6 JSON数组包装的原始JSON格式输出(json /数组

MySQL localhost:33060+ ssl world_x JS > shell.options.set('resultFormat','json/array')sql(“select *从城市countrycode = AUT”)[{" ID ": 1523年,“名字”:“维也纳”,“countrycode”:“AUT”、“区”:“维也纳”,“信息”:{“人口”:1608144}},{" ID ": 1524年,“名字”:“格拉茨”,“countrycode”:“AUT”、“区”:“施泰尔马克”,“信息”:{“人口”:240967}},{" ID ": 1525年,“名字”:“林兹”,“countrycode”:“AUT”、“区”:“北奥地利”,“信息”:{“人口”:188022}},{" ID ": 1526年,“名字”:“萨尔斯堡”、“countrycode”:“AUT”、“区”:“萨尔斯堡”,“信息”:{“人口”:144247}},{" ID ": 1527,“名字”:“因斯布鲁克”、“CountryCode”:“AUT”、“区”:“Tiroli”,“信息”:{“人口”:111752}},{" ID ": 1528年,“名字”:“克拉根福”、“CountryCode”:“AUT”、“区”:“克恩顿州”,“信息”:{“人口”:91141}}]6行组(0.0032秒)