Aanchal Agarwal
Aanchal Agarwal

Reputation: 57

Combine two JSONPath queries

I need to combine the following JSONPath queries into a single query.

mapping_id = 1.1

jsonpath_expression = parse(f'HEADERS[*].SUB_HEADERS[?(@.HEADER_ID=={mapping_id}) & (@.HEADER_IXML_PARAID != "")].HEADER_IXML_PARAID, SECTION_TEXT_PARAID_LIST')

jsonpath_expression = parse(f'HEADERS[?(@.HEADER_ID=={mapping_id}) & (@.HEADER_IXML_PARAID != "")].HEADER_IXML_PARAID, SECTION_TEXT_PARAID_LIST')

Following is the JSON format that I need to get the information from:

{
    "HEADERS": [
        {
            "HEADER_ID": 1,
            "HEADER_NAME": "HEADING 1 TEXT",
            "HEADER_IXML_PARAID": "116BACE8",
            "SECTION_TEXT_PARAID_LIST": [],
            "SUB_HEADERS": [
                {
                    "HEADER_ID": 1.1,
                    "HEADER_NAME": "Therapeutic indications",
                    "HEADER_IXML_PARAID": "0A863DE3",
                    "SECTION_TEXT_PARAID_LIST": [
                        "4E01355B",
                        "132F90DD"
                    ],
                    "SUB_HEADERS": []
                }],
        },
        {
            "HEADER_ID": 2,
            "HEADER_NAME": "HEADING 2 TEXT",
            "HEADER_IXML_PARAID": "116BACE8",
            "SECTION_TEXT_PARAID_LIST": [],
            "SUB_HEADERS": []
        }
    ]
}

Right now, this is the approach that I am using, but I would prefer to combine the two.

from jsonpath_ng.ext import parse

jsonpath_expression = parse(f'HEADERS[?(@.HEADER_ID=={mapping_id}) & (@.HEADER_IXML_PARAID != "")].HEADER_IXML_PARAID, SECTION_TEXT_PARAID_LIST')
match = jsonpath_expression.find(self.tagged_sections)
if not match:
    jsonpath_expression = parse(f'HEADERS[*].SUB_HEADERS[?(@.HEADER_ID=={mapping_id}) & 
                                (@.HEADER_IXML_PARAID != "")].HEADER_IXML_PARAID, SECTION_TEXT_PARAID_LIST')
    match = jsonpath_expression.find(self.tagged_sections)

Upvotes: 0

Views: 866

Answers (1)

gregsdennis
gregsdennis

Reputation: 8428

I'm not sure which library that you're using, but many implementations support comma-delimited keys: ['foo','bar']. You could use this along with a recursive descent operator: ...

$..[?(@.HEADER_ID==8.3) & (@.HEADER_IXML_PARAID != "")].HEADER_IXML_PARAID, SECTION_TEXT_PARAID_LIST

You also have some syntax that would be considered non-standard, though it may be supported by your library. I would right it as

$..                                                        // recursive descent
  [?(@.HEADER_ID==8.3 && @.HEADER_IXML_PARAID != "")]      // item filter
  ['HEADER_IXML_PARAID', 'SECTION_TEXT_PARAID_LIST']       // prop selection

The last thing is what I imagine you're asking for: combining the filter expression.

$..
  [?(@.HEADER_ID==1 || @.HEADER_ID==8.3) && @.HEADER_IXML_PARAID != "")]
  ['HEADER_IXML_PARAID', 'SECTION_TEXT_PARAID_LIST']

Be aware though that this will only select HEADER_IXML_PARAID, and SECTION_TEXT_PARAID_LIST and return all of the values in a collection; it can't ensure that they remain associated with each other.

Upvotes: 1

Related Questions