JsonPath は、文字列の式を記述して JSON から要素を取得する方法です。 取得対象の JSON 構造に合わせて式を記述する必要があります。
※こちらの記入例は、変換「JSON展開」ではご利用いただけませんのでご注意ください。
サンプル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
}
],
"fountain pen": {
"color": "silver",
"price": 200.30
}
},
"expensive": 10
}
設定例
上記のサンプルJSONデータを元に設定例を下記へ記載します。
JSONPath | JSONPathの意味 | 出力結果 |
$.store.book[*].author | すべての本の著者 | Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien |
$..author | すべての著者 | Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien |
$.store.* | 店内にあるものすべて | [{"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}] {"color":”silver","price":200.3} |
$.store..price | 店内にあるものすべての値段 | 8.95 12.99 8.99 22.99 200.3 |
$..book[2] | 3冊目の本 | {"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99} |
$..book[0,1] | 最初の2冊 | {"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} |
$.store.book[?(@.price==8.95)] | 価格が 8.95 に等しいすべての書籍をフィルター | {"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95} |
$.store.book[?(@.category=='fiction')] | フィクション カテゴリに属するすべての書籍をフィルタリング | {"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} |
$.store.book[?(@.author=~/tolkien/i)] | 正規表現に一致するすべての書籍 ※/.../i - 大文字・小文字無視モード | {"author":"J. R. R. Tolkien","price":22.99,"isbn":"0-395-19395-8","category":"fiction","title":"The Lord of the Rings"} |
JsonPath 演算子
オペレーター | 説明 |
---|---|
$ | root:最上位に位置する要素 |
@ | current node:フィルター結果の対象要素 |
* | wildcard:すべての要素 |
.. | recursive descent:キーが一致するすべての要素 |
.<name> | child:子要素 |
['<name>' (, '<name>')] | child/children:子要素(単数、複数) |
[<number> (, <number>)] | index/indices:インデックス(単数、複数) |
[start:end] | array slice:配列要素 |
[?(<expression>)] | filter expression:フィルター |