$json.path¶
Description¶
Évalue une expression JSON-Path
Syntaxe¶
$json.path( json , expression [ , defaultValue ] )
Paramètres¶
jsonstring- Arbre json ou clé précédemment obtenue via
$json.load expressionstring- Expression json-path à évaluer
defaultValueany- Valeur par défaut à retourner en cas d'erreur, c.-à-d. typiquement si l'expression référence un élément qui n'existe pas
Retour¶
Valeur correspondant à l'expression. En cas d'erreur (path non trouvé),
c'est la valeur par défaut qui est retournée. Si aucun arbre json n'est
donné, la méthode retourne null.
Exemple¶
L'exemple ci-dessous extrait l'auteur de chaque livre. Cet exemple reprend le premier exemple disponible sur la page https://github.com/json-path/JsonPath.
var json = `{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}`::t;
var array = $json.path(&json, "$.store.book[*].author");