h2non / jsonpath-ng

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

Unclear how to filter same key-set on multiple levels of a tree #94

Open bionicles opened 2 years ago

bionicles commented 2 years ago

Hi, I'm trying to wrangle some health data and need to match on abstract path patterns, if that makes sense.

Could you expand the docs and docstrings a bit to demonstrate more advanced usage?

ex: how do you filter objects with a set of keys? how do you apply that filter all over the whole tree? how do you return the matches with the resourceType from the parent?

I want to walk a tree and save (resourceType, system, code, display) tuples for each code, but the stuff with {system, code, display} mappings are usually nested inside the resource instances

do you know a nice way to filter on all subtrees with [system, code, display] as keys?

x = (
        {'resourceType': 'Patient', 'id': 'example', 'name': {'prefix': 'Dr.', 'given': ['Bob', 'B.'], 'family': 'Bobbins'}},
        {
            'resourceType': 'Observation',
            'id': 'example_code_and_valueQuantity',
            'subject': {'reference': 'Patient/example'},
            'code': {'coding': [{'system': 'http://loinc.org', 'code': '29463-7', 'display': 'Body weight'}]},
            'valueQuantity': {'value': '70', 'unit': 'kg', 'system': 'http://unitsofmeasure.org', 'code': 'kg'}
        },
        {
            'resourceType': 'Observation',
            'id': 'example_component',
            'subject': {'reference': 'Patient/example'},
            'code': {'coding': [{'system': 'http://loinc.org', 'code': '55284-4', 'display': 'Blood Pressure'}]},
            'component': (
                {
                    'code': {
                        'coding': {'system': 'http://loinc.org', 'code': '8462-4', 'display': 'Diastolic Blood Pressure'},
                        'text': 'Diastolic Blood Pressure'
                    },
                    'valueQuantity': {
                        'value': 84.51637275024623,
                        'unit': 'mm[Hg]',
                        'system': 'http://unitsofmeasure.org',
                        'code': 'mm[Hg]'
                    }
                },
                {
                    'code': {
                        'coding': {'system': 'http://loinc.org', 'code': '8480-6', 'display': 'Systolic Blood Pressure'},
                        'text': 'Systolic Blood Pressure'
                    },
                    'valueQuantity': {
                        'value': 119.11425717022348,
                        'unit': 'mm[Hg]',
                        'system': 'http://unitsofmeasure.org',
                        'code': 'mm[Hg]'
                    }
                }
            )
        }
    )
y = (("Observation", "https://loinc.org", "55284-4", "Blood Pressure"), ...)

BTW i think your project could help a lot of people because most medical data right now is nested json, thank you for your work and thank you for taking the time to read this