'How to filter keys by the value of another elements with JOLT?
I have the following input JSON:
{
"2021" : [ "a", "b", "c" ],
"2022" : [ "d", "e", "f" ],
"2023" : [ "g", "h", "i" ],
"2024" : [ "j", "k", "l" ],
"2025" : [ "m", "n", "o" ],
"year" : "2022"
}
And I need to filter the keys that are equal to the value of 'year'. In this case, I just need the key '2022'. Something like that:
{
"results_this_year" : [ "d", "e", "f" ],
"year" : "2022"
}
Solution 1:[1]
This can be done like this:
[
{
"operation": "shift",
"spec": {
"year": {
"*": {
"@(2,&)": "&"
},
"@": "&"
}
}
},
// just to sort
{
"operation": "sort",
"spec": {
"*": ""
}
}
]
"@(2,&)": "&" - traverses backwards up the source JSON and mapping the values ??from the top level to the value from "year".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |