json_extract() entra all'interno di un documento JSON ed estrae i valori. Fornisci il JSON, quindi uno o più percorsi che descrivono ciò che vuoi ottenere.
Sintassi
json_extract(X,P1,P2,...) Esempi
SELECT json_extract('{ "a" : 1 }', '$'); Risultato:
{"a":1} Il percorso $ si riferisce all'intero documento. Con qualcosa di più grande:
SELECT json_extract('{ "dogs" : [ { "name" : "Wag", "scores" : [ 7, 9 ] }, { "name" : "Bark", "scores" : [ 3, 4, 8, 7 ] }, { "name" : "Woof", "scores" : [ 3, 2, 1 ] } ] }', '$' ); Risultato:
{"dogs":[{"name":"Wag","scores":[7,9]},{"name":"Bark","scores":[3,4,8,7]},{"name":"Woof","scores":[3,2,1]}]} Solo l'array:
SELECT json_extract('{ "dogs" : [ { "name" : "Wag", "scores" : [ 7, 9 ] }, { "name" : "Bark", "scores" : [ 3, 4, 8, 7 ] }, { "name" : "Woof", "scores" : [ 3, 2, 1 ] } ] }', '$.dogs' ); Risultato:
[{"name":"Wag","scores":[7,9]},{"name":"Bark","scores":[3,4,8,7]},{"name":"Woof","scores":[3,2,1]}] Un solo elemento di quell'array:
SELECT json_extract('{ "dogs" : [ { "name" : "Wag", "scores" : [ 7, 9 ] }, { "name" : "Bark", "scores" : [ 3, 4, 8, 7 ] }, { "name" : "Woof", "scores" : [ 3, 2, 1 ] } ] }', '$.dogs[1]' ); Risultato:
{"name":"Bark","scores":[3,4,8,7]} Gli array partono da zero, quindi [1] indica il secondo cane.
Un livello più in profondità:
SELECT json_extract('{ "dogs" : [ { "name" : "Wag", "scores" : [ 7, 9 ] }, { "name" : "Bark", "scores" : [ 3, 4, 8, 7 ] }, { "name" : "Woof", "scores" : [ 3, 2, 1 ] } ] }', '$.dogs[1].name' ); Risultato:
Bark
Più percorsi contemporaneamente
SELECT json_extract('{ "dogs" : [ { "name" : "Wag", "scores" : [ 7, 9 ] }, { "name" : "Bark", "scores" : [ 3, 4, 8, 7 ] }, { "name" : "Woof", "scores" : [ 3, 2, 1 ] } ] }', '$.dogs[0].name', '$.dogs[1].name', '$.dogs[2].name' ); Risultato:
["Wag","Bark","Woof"]
Più percorsi producono sempre un array JSON.
Un percorso che non esiste
Prima rendi visibili i valori null:
.nullvalue NULL Il comando dot .nullvalue sostituisce una stringa ai risultati null: è uno dei diversi modi per gestirli in SQLite. Poi:
SELECT json_extract('{ "dogs" : [ { "name" : "Wag", "scores" : [ 7, 9 ] }, { "name" : "Bark", "scores" : [ 3, 4, 8, 7 ] }, { "name" : "Woof", "scores" : [ 3, 2, 1 ] } ] }', '$.cats' ); Risultato:
NULL
Casi di errore
SELECT json_extract('{ "dogs" : [ { "name" : "Wag", "scores" : [ 7, 9 ] } ] }', 'dogs' ); Risultato:
Runtime error: JSON path error near 'dogs'
SELECT json_extract('{ "Dogs" : }', '$' ); Risultato:
Runtime error: malformed JSON
Tipi restituiti
La documentazione di SQLite lo spiega così:
If only a single path P1 is provided, then the SQL datatype of the result is NULL for a JSON null, INTEGER or REAL for a JSON numeric value, an INTEGER zero for a JSON false value, an INTEGER one for a JSON true value, the dequoted text for a JSON string value, and a text representation for JSON object and array values. If there are multiple path arguments (P1, P2, and so forth) then this routine returns SQLite text which is a well-formed JSON array holding the various values.
Una nota sulla compatibilità con MySQL
La documentazione segnala anche una differenza sottile:
The MySQL version of json_extract() always returns JSON. The SQLite version of json_extract() only returns JSON if there are two or more PATH arguments (because the result is then a JSON array) or if the single PATH argument references an array or object. In SQLite, if json_extract() has only a single PATH argument and that PATH references a JSON null or a string or a numeric value, then json_extract() returns the corresponding SQL NULL, TEXT, INTEGER, or REAL value.
In pratica la discrepanza emerge solo quando il percorso punta a una stringa o a un valore null.