JSONPath Tester
JSONPathの表現をJSONデータに対してテストします。
このツールについて
JSONPathはJSON用のクエリ言語で、XMLのXPathに相当します。JSONPathテスターは、開発者やデータアナリストがJSONPathの式をサンプルJSON文書に対して直接テストし、検証・デバッグするのを支援します。API、設定ファイル、複雑なデータ構造を扱う際に、正確なパスナビゲーションが必要な場合、このツールは推測の余地をなくします。
ツールを使うには、入力フィールドにJSON データを貼り付け、式フィールドにJSONPath式を入力します。入力しながら、テスターはすぐにどの要素が式に一致するか、またそれらの値を表示します。$. はルートアクセス、.property はオブジェクトプロパティ、[0] は配列のインデックス、[*] はすべての配列要素を選択するなど、一般的なパターンが多くあります。結果にはすべての一致するパスと値が表示され、必要な値を正確に抽出するまで簡単にクエリを改善できます。
よくある質問
コード実装
# pip install jsonpath-ng
from jsonpath_ng import parse
data = {
"store": {
"book": [
{"title": "Moby Dick", "author": "Herman Melville", "price": 8.99},
{"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "price": 12.99},
{"title": "1984", "author": "George Orwell", "price": 6.99}
]
}
}
# Match all book titles
expr = parse("$.store.book[*].title")
titles = [match.value for match in expr.find(data)]
print(titles)
# ['Moby Dick', 'The Great Gatsby', '1984']
# Filter books cheaper than $10
expr2 = parse("$.store.book[?(@.price < 10)]")
cheap_books = [match.value for match in expr2.find(data)]
print(cheap_books)
# [{'title': 'Moby Dick', ...}, {'title': '1984', ...}]
# Recursive descent: find all authors anywhere in the document
expr3 = parse("$..author")
authors = [match.value for match in expr3.find(data)]
print(authors)
# ['Herman Melville', 'F. Scott Fitzgerald', 'George Orwell']Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.