Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get all matching fields, the path is uncertain #313

Open
litao09h opened this issue Mar 1, 2023 · 2 comments
Open

how to get all matching fields, the path is uncertain #313

litao09h opened this issue Mar 1, 2023 · 2 comments

Comments

@litao09h
Copy link

litao09h commented Mar 1, 2023

Example:

{
    "k1": {
        "text": "m1",
        "v": 1
    },
    "k2": {
        "child": {
            "text": "n2",
            "v": 1
        }
    },
    "k3": {
        "v": 1
    }
}

example in jsonPath:
express: $..text
output: ["m1","n2"]

@liveFreeOrCode
Copy link

@litao09h, were you ever able to figure this out? I'm looking to do the same thing

@volans-
Copy link

volans- commented Aug 2, 2023

@litao09h @liveFreeOrCode
As far as I know it's not possible to do it for the generic case of arbitrary depths.
But if you know in advance the possible depths where the field is present though then it becomes doable with a bit of a verbose query. For example for the specific case above with depth 1 and 2 a query of the form:

[@values.#.text,@values.#.@values.#.text].@flatten:{"deep":true}

gives you:

["m1","n2"]

The [] around is a multipaths to create a new object, and the @flatten is needed to remove all the empty lists for the part of the query for depth two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants