Skip to content

Is it possible to get None when the value is not present in a specific array item? #40

Open
@stellasia

Description

@stellasia

Hi and thank you for putting together this really helpful package!

I have a use case where I would like to keep matched objects synchronized.

For instance, using the data from the documentation:

from jsonpath import parse
data = {
    "goods": [
        {"price": 100, "category": "Comic book"},
        {"price": 200, "category": "magazine"},
        {"price": 200, "no category": ""}
    ],
    "targetCategory": "book"
}

I would like to extract price and category keys into lists, but lists must have same lengths.

The following expression:

parse("$.goods[*].category").find(data)

outputs all matching items (which is already super cool :)) :

['Comic book', 'magazine',]

Is there a way to add 'None' for array items that do not contain the category key? I.e. having the following output:

['Comic book', 'magazine', None]

(Or any other way to achieve the desired result?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions