πŸ› οΈToolsShed

JSONPath Tester

Test JSONPath expressions against JSON data.

Frequently Asked Questions

Code Implementation

# 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.