JMESPath search nested data for key
Asked Answered
C

0

6

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
Cadre answered 25/2, 2019 at 12:16 Comment(2)
Did you find a solution?Gargan
@Gargan no sorry... I gave up and honestly can't remember what I did instead.Cadre

© 2022 - 2024 — McMap. All rights reserved.