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

Easy way to select specific objects in an array #256

Open
thelaoshi opened this issue Apr 19, 2022 · 2 comments
Open

Easy way to select specific objects in an array #256

thelaoshi opened this issue Apr 19, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@thelaoshi
Copy link

Lets say I have a array of objects and want so select a specific one, what is the best way to do it?

example:
[{ "name": "obj1", "value": 10},{ "name": "obj2", "value: 20}]

how can I easily select obj2 or easily test if obj2 has a value over 10 and then add a 3rd object for example? Right now the only idea i have is to make a for with if else and that is quite cumbersome compared to jsonPath syntax like [?@.name == "obj2"].value

@larsga larsga added the enhancement New feature or request label Jun 15, 2022
@larsga
Copy link
Collaborator

larsga commented Jun 15, 2022

I agree that the for loop is cumbersome for this, and I have been thinking about supporting something similar to the JsonPath syntax. It might still take a while to get that into the language, though.

@TheChatty
Copy link

TheChatty commented Feb 12, 2024

I also vouch for better support.

I have a source with multiple name objects which I want to select.

{
  "NAME": [
    {
      "TYPE": "FIRST",
      "content": "Peter"
    },
    {
      "TYPE": "LAST",
      "content": "Klöppel"
    }
  ]
}

It's super simple with JsonPath:

.NAME[?(@.TYPE=='FIRST')].content

But with JSLT I have to use the for-loop-workaround. And even worse I cannot use it inline.

{
"firstName": [for (.NAME) .content if(.TYPE=="FIRST")][0],
}

I have to define this upfront.

let firstName = [for (.NAME) .content if(.TYPE=="FIRST")]
{
"firstName": $firstName[0],
}

There is a workaround for this:

{
"firstName": fallback([for (.NAME) .content if(.TYPE=="FIRST")], [""])[0]
}

@larsga: What is your say on this matter?

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

No branches or pull requests

3 participants