h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
564 stars 85 forks source link

Cannot filter array by non-existent field. #151

Open Klwilson2272 opened 7 months ago

Klwilson2272 commented 7 months ago

Example data set returned by querying all records.

qv: $.StatementsOfIncome.RevenueFromContractWithCustomerExcludingAssessedTax[*] version: 1.6.0

{'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2022-09-25', 'endDate': '2023-09-30'}, 'segment': {'dimension': 'srt:ProductOrServiceAxis', 'value': 'us-gaap:ProductMember'}, 'value': '298085000000'}
{'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2022-09-25', 'endDate': '2023-09-30'}, 'value': '383285000000'}

I am interested in selecting the rows without a 'segment'. However, when I specify the filter:

match = xpathQuery(f"$.{section}.{property}[?!(@.segment)]")
[ print(m.value) for m in match]

I get a error on unexpected character: !

And if I alter this to attempt:

match = xpathQuery(f"$.{section}.{property}[?(@.segment==null)]")
[ print(m.value) for m in match]

The results are empty.

Proving the syntax is correct. I change the query to:

match = xpathQuery(f"$.{section}.{property}[?(@.segment!=null)]")
[ print(m.value) for m in match]

This returns just the row with a segment field.

{'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2022-09-25', 'endDate': '2023-09-30'}, 'segment': {'dimension': 'srt:ProductOrServiceAxis', 'value': 'us-gaap:ProductMember'}, 'value': '298085000000'}

How can I tell the query to only respond with rows without a segment? expect to be able to query: {'decimals': '-6', 'unitRef': 'usd', 'period': {'startDate': '2022-09-25', 'End Date': '2023-09-30'}, 'value': '383285000000'}

This seems so simple, but it seems the feature is missing.

In python I can wrap the query, but this circumvents the upper layers of the application specifying the selection criteria.

Klwilson2272 commented 7 months ago

Java JsonPath library handles this by setting a optional configuration to return NULL for the leaf. https://stackoverflow.com/questions/48109347/json-path-field-null-checking-expression