I am trying to find a way to extract JSON attributes using wildcard selection with jq. Is there any way to do it? I have the following JSON file. The fields can be dynamic for each system. I want to map Core with its "input" temperature (tempX_input).
{
"Core 0":{
"temp2_input": 52.000,
"temp2_max": 100.000,
"temp2_crit": 100.000,
"temp2_crit_alarm": 0.000
},
"Core 1":{
"temp3_input": 53.000,
"temp3_max": 100.000,
"temp3_crit": 100.000,
"temp3_crit_alarm": 0.000
}
}
Result:
{
"Core 0": 52.000,
"Core 1": 53.000
}
CodePudding user response:
You could use:
with_entries(.value |= (with_entries(select(.key | endswith("_input"))) | to_entries[].value ))
Using with_enries
so we can get the desired keys using endswith("_input")
The above will generate:
{
"Core 0": 52,
"Core 1": 53
}
As you can test in this online demo
CodePudding user response:
Using regular expression :
jq 'map_values(
to_entries[] |
select(.key |
test("temp[[:digit:]] _input")).value
)' input.json