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

Subqueries #29

Closed
michaelko opened this issue Oct 22, 2012 · 3 comments
Closed

Subqueries #29

michaelko opened this issue Oct 22, 2012 · 3 comments

Comments

@michaelko
Copy link

Suggest you have this code (from selectjson)

{
    "name": {
        "first": "Lloyd",
        "last": "Hilaiel"
    },
    "favoriteColor": "yellow",
    "languagesSpoken": [
        {
            "lang": "Bulgarian",
            "level": "advanced"
        },
        {
            "lang": "English",
            "level": "native",
            "preferred": true
        },
        {
            "lang": "Spanish",
            "level": "beginner"
        }
    ],
    "seatingPreference": [
        "window",
        "aisle"
    ],
    "drinkPreference": [
        "whiskey",
        "beer",
        "wine"
    ],
    "weight": 172
}

And you want to select all languag names you can do that with
.languagesSpoken[].lang

If you want to select only the preferred ones you can do something like
[.languagesSpoken[] | if .preferred then .lang else 0 end] but that is imo not satisfying since the list now contains a placeholder. I suggest <> a subquery syntax.

exp1<exp2> would be equivalent to [exp1[] | if exp2 then . else **Nothing** end]

languagesSpoken<.preferred>.lang would be "English"

Since it can only be applied to a array i suggest it should be languagesSpoken<.preferred> rather than ``languagesSpoken[]<.preferred>`

@stedolan
Copy link
Contributor

**Nothing** is in fact implemented, and it's called empty. You can write:

[.languagesSpoken[] | if .preferred then .lang else empty end]

You can use the builtin "select" function to write it as:

[.languagesSpoken[] | select(.preferred) | .lang]

@michaelko
Copy link
Author

Yes, please. Man, this is awesome.

@michaelko
Copy link
Author

And a way more elegant.

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

No branches or pull requests

3 participants