Reputation: 3152
I have a json with attribute names that start with some prefixes I need to ignore, and end with the actual field name:
{
"context": {
"values": {
"group1:0:foo": "08119037",
"group1:0:checkbox": [
"2",
"3"
]
}
}
}
I can extract the exact field group1:0:foo
, but not "the field that ends with :foo":
{
"foo": context.values.`group1:0:foo`
}
In the JSONata exerciser: https://try.jsonata.org/uqDfozN8r
Upvotes: 2
Views: 654
Reputation: 3152
Solution by shrickus:
$spread(context.values)[$match($keys($)[0], /.*:foo/)] {
$keys($)[0].$split(":")[2]: $.*
}
-> Accepted answer because it avoids the lookup.
Upvotes: 2
Reputation: 3152
One possible solution: https://try.jsonata.org/9vruWw6HR
{
"foo": $lookup(context.values, context.values.$keys()[$contains($, 'foo')])
}
Result:
{
"foo": "08119037"
}
Upvotes: 2