In MariaDB, JSON_DETAILED() è una funzione incorporata che prende un documento JSON e lo restituisce in un formato più leggibile.
Questo è a volte indicato come abbellire il documento. È simile a JSON_PRETTY() di MySQL funzione.
Per l'effetto opposto (cioè per condensare un documento JSON), usa JSON_COMPACT() funzione.
Sintassi
La sintassi è questa:
JSON_DETAILED(json_doc[, tab_size])
Dove json_doc è il documento JSON e tab_size è un valore facoltativo che specifica la dimensione della tabulazione/rientri.
Esempio
Ecco un esempio da dimostrare.
SET @json_document = '{ "name": "Wag", "type": "Dog", "weight": 20 }';
SELECT JSON_DETAILED(@json_document); Risultato:
+----------------------------------------+
| JSON_DETAILED(@json_document) |
+----------------------------------------+
| {
"name": "Wag",
"type": "Dog",
"weight": 20
} |
+----------------------------------------+ Il documento originale è tutto su una riga, senza tabulazioni/rientri o altra formattazione.
Il risultato è distribuito su più righe e contiene tabulazioni/rientri, il che rende il documento più facile da leggere per noi umani.
Strutture nidificate
Ecco un altro paio di esempi, questa volta con strutture nidificate:
SET @json_document = '{ "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] }';
SELECT JSON_DETAILED(@json_document); Risultato:
+---------------------------------------+
| JSON_DETAILED(@json_document) |
+---------------------------------------+
| {
"_id": 1,
"awards":
[
"Top Dog",
"Best Dog",
"Biggest Dog"
]
} |
+---------------------------------------+ E un altro:
SET @json_document = '{ "_id" : 2, "specs" : { "height" : 400, "weight" : 15, "color" : "brown" } }';
SELECT JSON_DETAILED(@json_document); Risultato:
+---------------------------------------+
| JSON_DETAILED(@json_document) |
+---------------------------------------+
| {
"_id": 2,
"specs":
{
"height": 400,
"weight": 15,
"color": "brown"
}
} |
+---------------------------------------+ Dimensione scheda
Hai anche la possibilità di specificare la dimensione della scheda. Per fare ciò, passa la dimensione della scheda desiderata come secondo argomento.
Esempio
SET @json_document = '{ "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] }';
SELECT JSON_DETAILED(@json_document, 1); Risultato:
{
"_id": 1,
"awards":
[
"Top Dog",
"Best Dog",
"Biggest Dog"
]
} Ed eccolo di nuovo, ma con una dimensione della scheda più grande:
SET @json_document = '{ "_id" : 1, "awards" : [ "Top Dog", "Best Dog", "Biggest Dog" ] }';
SELECT JSON_DETAILED(@json_document, 10); Risultato:
{
"_id": 1,
"awards":
[
"Top Dog",
"Best Dog",
"Biggest Dog"
]
} Documento JSON più grande
Ecco un esempio con un documento JSON leggermente più grande.
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); Risultato:
{
"_id": 1,
"name": "Wag",
"details":
{
"type": "Dog",
"weight": 20,
"awards":
{
"Florida Dog Awards": "Top Dog",
"New York Marathon": "Fastest Dog",
"Sumo 2020": "Biggest Dog"
}
}
} Argomento nullo
Se l'argomento è NULL , il risultato è NULL :
SELECT JSON_DETAILED(null); Risultato:
+---------------------+ | JSON_DETAILED(null) | +---------------------+ | NULL | +---------------------+
Conteggio parametri errato
Non fornire argomenti genera un errore:
SELECT JSON_DETAILED(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DETAILED'
È lo stesso quando fornisci troppi argomenti:
SELECT JSON_DETAILED('{ "a": 1}', 1, 2); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DETAILED'