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

Traverse plain Python classes/objects as well as JSON dicts #185

Open
athewsey opened this issue Nov 14, 2024 · 1 comment
Open

Traverse plain Python classes/objects as well as JSON dicts #185

athewsey opened this issue Nov 14, 2024 · 1 comment

Comments

@athewsey
Copy link

Hi & thanks for your work sharing this library!

I have a use-case where I'd like to be able to fetch data from either a raw API response (in dicts, lists, and other JSON-native types), OR from an already-parsed Pydantic model (even better if arbitrary Python classes/objects supported too).

It seems from the Descendents.find implementation like jsonpath-ng explicitly does not support fetching data from plain Python objects (for example using getattr(datum.value, field) instead of datum.value[field]). Would it be possible to enable this?

It is fair that there are some potentially funky edge cases here, because you'd be able to retrieve properties & methods from native objects (For example $.num.real.real.real.real... on {"num": 13})... So maybe it'd be necessary to put the ability to traverse plain objects behind a configuration option in parse(), and leave it turned off by default? That'd still un-block our use-case.

@athewsey athewsey changed the title Work with plain Python classes/objects as well as JSON dicts Traverse plain Python classes/objects as well as JSON dicts Nov 14, 2024
@athewsey
Copy link
Author

For now my workaround to this is to use JSONPath on obj.model_dump() (since this Pydantic method supports recursively converting possibly-nested models to plain dicts)... Which is okay, but wouldn't work for any use-cases hoping to use other Python objects that don't have this interface.

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

No branches or pull requests

1 participant