Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple query projection #61

Merged
merged 11 commits into from
Jul 2, 2024
Merged

Add simple query projection #61

merged 11 commits into from
Jul 2, 2024

Conversation

jg-rp
Copy link
Owner

@jg-rp jg-rp commented Jun 29, 2024

This PR adds a select method to the query iterator interface that returns a projection of each JSONPath match by selecting a subset of its values.

Given example data from Stefan Goessner's original JSONPath article, this example selects just the title and price fields for each book with an ISBN.

import json
import jsonpath

# data = ...

it = jsonpath.query("$..book[?(@.isbn)]", data).select("title", "price")
print(json.dumps(list(it), indent=4))

Output

[
    {
        "title": "Moby Dick",
        "price": 8.99
    },
    {
        "title": "The Lord of the Rings",
        "price": 22.99
    }
]

Normally, without the call to select, we'd get the entire book object.

# ...
it = jsonpath.query("$..book[?(@.isbn)]", data).values()
print(json.dumps(list(it), indent=4))

Output

[
    {
        "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
    }
]

We can select nested values too.

import json
import jsonpath

data = {
    "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,
                "foo": {"bar": 42},
            },
        ],
        "bicycle": {"color": "red", "price": 19.95},
    }
}


it = jsonpath.query("$..book[?(@.isbn)]", data).select("title", "price", "foo.bar")
print(json.dumps(list(it), indent=4))

Output

[
    {
        "title": "Moby Dick",
        "price": 8.99
    },
    {
        "title": "The Lord of the Rings",
        "price": 22.99,
        "foo": {
            "bar": 42
        }
    }
]

@jg-rp jg-rp marked this pull request as draft June 29, 2024 08:31
@jg-rp jg-rp marked this pull request as ready for review July 2, 2024 18:57
@jg-rp jg-rp merged commit c28196a into main Jul 2, 2024
36 checks passed
@jg-rp jg-rp deleted the projection branch July 2, 2024 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant