MySQL Shell 8.4
MySQL Shell provides a number of JSON format options to print result sets:
json
or json/pretty
These options both produce pretty-printed JSON.
ndjson
or json/raw
These options both produce raw JSON delimited by newlines.
json/array
This option produces raw JSON wrapped in a JSON array.
You can select these output formats by starting MySQL Shell
with the
--result-format=
command line option, or setting the MySQL Shell configuration
option value
resultFormat
.
In batch mode, to help integrate MySQL Shell with external
tools, you can use the --json
option to control JSON wrapping for all output when you start
MySQL Shell from the command line. When JSON wrapping is turned
on, MySQL Shell generates either pretty-printed JSON (the
default) or raw JSON, and the value of the
resultFormat
MySQL Shell configuration
option is ignored. For instructions, see
Section 5.7.5, “JSON Wrapping”.
Example 5.4 Output in Pretty-Printed JSON Format (json
or
json/pretty
)
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)
Example 5.5 Output in Raw JSON Format with Newline Delimiters
(ndjson
or json/raw
)
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)
Example 5.6 Output in Raw JSON Format Wrapped in a JSON Array
(json/array
)
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)