jslt
jslt copied to clipboard
Easy way to select specific objects in an array
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 [[email protected] == "obj2"].value
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.
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?