Given a collection (not an array) like this:
{
"field1": {
"type": "string",
"value": "test"
},
"field2": {
"type": "array",
"value": []
},
"field3": {
"type": "string",
"value": "test"
},
"field4": {
"type": "array",
"value": []
}
}
how do I end up with just (filtering on type="array"
):
{
"field2": {
"type": "array",
"value": []
},
"field4": {
"type": "array",
"value": []
}
}
Given a collection (not an array) like this:
{
"field1": {
"type": "string",
"value": "test"
},
"field2": {
"type": "array",
"value": []
},
"field3": {
"type": "string",
"value": "test"
},
"field4": {
"type": "array",
"value": []
}
}
how do I end up with just (filtering on type="array"
):
{
"field2": {
"type": "array",
"value": []
},
"field4": {
"type": "array",
"value": []
}
}
Share
Improve this question
edited Mar 24 at 17:25
jonrsharpe
122k30 gold badges268 silver badges475 bronze badges
asked Mar 24 at 17:23
L SzijL Szij
11 bronze badge
1 Answer
Reset to default 0The JSONata wildcard operator will select the values of all fields in the context object.
Option 1
$map($spread($), function($x){
$x.*.type = 'array' ? {
$keys($x): $x.*
}
})
JSONata Playground: https://jsonatastudio/playground/f5648325
Option 2
$.*[type="array"]
The output will look like:
[
{
"type": "array",
"value": []
},
{
"type": "array",
"value": []
}
]
JSONata Playground: https://jsonatastudio/playground/3007924a