jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

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

Open stellasia opened this issue 3 years ago • 0 comments

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?)

stellasia avatar Jan 19 '22 08:01 stellasia