jmespath / jmespath.js

Javascript implementation of JMESPath, a query language for JSON
http://jmespath.org
Other
783 stars 97 forks source link

How to search in this case #71

Open jet10000 opened 3 years ago

jet10000 commented 3 years ago

json

{
    1: [
        01: {
            content: "this one"
        },
        02: {
            content: "this two"
        }
    ],
    2: [
        01: {
            content: "this three"
        },
        02: {
            content: "this four"
        }
    ]
}

I want to filter contains "two" keyword and want to return this type of result

{
    1: [
        02: {
            content: "this two"
        }
    ]
}
darrenmothersele commented 3 years ago

Something like this?

values(@)[*].values(@)[?contains(content, 'two')][]

https://codesandbox.io/s/cranky-flower-4ufz4?file=/src/index.js

returns:

[
  {
    "content": "this two"
  }
]

I can't think of a way of returning the exact structure you are looking for using standard JMESPath. But you might manage it using an extension like this: https://github.com/daz-is/jmespath-plus

jet10000 commented 3 years ago

I expect the returned result can retain the original json hierarchy, only need to exclude entries that do not contain keywords.