JSONPath Tester
针对JSON数据测试JSONPath表达式。
关于此工具
JSONPath是用于JSON数据的查询语言,类似于XML的XPath。JSONPath测试工具帮助开发人员和数据分析师直接针对示例JSON文档测试和验证JSONPath表达式。在处理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.