[Python] JMESPath search nested data for key

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use the Python Library for JMESPath to extract information from a large JSON file, a much simplified example is here:

    {
    "high_name":"test",
    "sections":[
    {
    "name":"section1",
    "item":"string1"
    },
    {
    "name":"section2",
    "item":"string2",
    "items_sub":[
    {
    "item":"deeper string1"
    }
    ]
    }
    ]
    }


    I am trying to get an output as such:

    {
    "high_name":"test",
    "sections":[
    {
    "name":"section1",
    "items":[
    "string1"
    ]
    },
    {
    "name":"section2",
    "items":[
    "string1",
    "deeper string1"
    ]
    }
    ]
    }


    The deeper string can be from 1 to 3 levels deep depending on the type of section. I need to search the keys to get all the matching item names.

    I have done this successfully using jsonpath-rw-ext but want to know if it is possible with JMESPath to remove the reliance on two libraries?

    I can't seem to find a way to search all sub keys regardless of their level in JMESPath.

    Working string for JSONPATH-RW-EXT:

    $.sections[*]..item


    Best attempt with JMESPath (doesn't work):

    sections[*].*.item

    Continue reading...

Compartilhe esta Página