No MariaDB,
JSON_DETAILED() é uma função interna que recebe um documento JSON e o retorna em um formato mais legível. Isso às vezes é chamado de embelezar o documento. É semelhante ao
JSON_PRETTY() do MySQL função. Para o efeito oposto (ou seja, para condensar um documento JSON), use o
JSON_COMPACT() função. Sintaxe
A sintaxe fica assim:
JSON_DETAILED(json_doc[, tab_size]) Onde
json_doc é o documento JSON e tab_size é um valor opcional que especifica o tamanho da tabulação/recuos. Exemplo
Aqui está um exemplo para demonstrar.
SET @json_document = '{ "name": "Wag", "type": "Dog", "weight": 20 }';
SELECT JSON_DETAILED(@json_document); Resultado:
+----------------------------------------+
| JSON_DETAILED(@json_document) |
+----------------------------------------+
| {
"name": "Wag",
"type": "Dog",
"weight": 20
} |
+----------------------------------------+ O documento original está todo em uma linha, sem tabulações/recuos ou outra formatação.
O resultado está espalhado por várias linhas e contém tabulações/recuos, o que torna o documento mais fácil de ler para nós humanos.
Estruturas aninhadas
Aqui estão mais alguns exemplos, desta vez com estruturas aninhadas:
SET @json_document = '{ "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] }';
SELECT JSON_DETAILED(@json_document); Resultado:
+---------------------------------------+
| JSON_DETAILED(@json_document) |
+---------------------------------------+
| {
"_id": 1,
"awards":
[
"Top Dog",
"Best Dog",
"Biggest Dog"
]
} |
+---------------------------------------+ E outro:
SET @json_document = '{ "_id" : 2, "specs" : { "height" : 400, "weight" : 15, "color" : "brown" } }';
SELECT JSON_DETAILED(@json_document); Resultado:
+---------------------------------------+
| JSON_DETAILED(@json_document) |
+---------------------------------------+
| {
"_id": 2,
"specs":
{
"height": 400,
"weight": 15,
"color": "brown"
}
} |
+---------------------------------------+ Tamanho da guia
Você também tem a opção de especificar o tamanho da guia. Para fazer isso, passe o tamanho de tabulação desejado como segundo argumento.
Exemplo
SET @json_document = '{ "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] }';
SELECT JSON_DETAILED(@json_document, 1); Resultado:
{
"_id": 1,
"awards":
[
"Top Dog",
"Best Dog",
"Biggest Dog"
]
} E aqui está novamente, mas com um tamanho de guia maior:
SET @json_document = '{ "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] }';
SELECT JSON_DETAILED(@json_document, 10); Resultado:
{
"_id": 1,
"awards":
[
"Top Dog",
"Best Dog",
"Biggest Dog"
]
} Documento JSON maior
Aqui está um exemplo com um documento JSON um pouco maior.
SET @json_document = '{"_id":1,"name":"Wag","details":{"type":"Dog","weight":20,"awards":{"Florida Dog Awards":"Top Dog","New York Marathon":"Fastest Dog","Sumo 2020":"Biggest Dog"}}}';
SELECT JSON_DETAILED(@json_document); Resultado:
{
"_id": 1,
"name": "Wag",
"details":
{
"type": "Dog",
"weight": 20,
"awards":
{
"Florida Dog Awards": "Top Dog",
"New York Marathon": "Fastest Dog",
"Sumo 2020": "Biggest Dog"
}
}
} Argumento nulo
Se o argumento for
NULL , o resultado é NULL :SELECT JSON_DETAILED(null); Resultado:
+---------------------+ | JSON_DETAILED(null) | +---------------------+ | NULL | +---------------------+
Contagem de parâmetros incorreta
Não fornecer argumentos resulta em um erro:
SELECT JSON_DETAILED(); Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DETAILED'
É o mesmo quando você fornece muitos argumentos:
SELECT JSON_DETAILED('{ "a": 1}', 1, 2); Resultado:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DETAILED'