Given the following JSON:
{
"one": "1",
"two": "2",
"flag": "f1 f2 f3",
"test one": "",
"test two": "",
"test three": ""
}
Is it possible to obtain the following result using jq?
{
"one": "1",
"two": "2",
"flags": ["f1", "f2", "f3"],
"tests": ["one", "two", "three"]
}
Three points are crucial:
-
Leave unchanged any field that is not
flagor starts withtest -
Transform
flag(space separated values) into an array -
Any field that starts with
testis added to an array (tests) where the value is the remaining part of the field’s name
>Solution :
You can use /= to update by splitting, startswith to match at the beginnning of a string, and to_entries and with_entries to manipulate entries involving the key name:
jq '
.flag /= " "
| .tests = (to_entries | map(.key | select(startswith("test "))[5:]))
| with_entries(select(.key | startswith("test ") | not))
'
{
"one": "1",
"two": "2",
"flag": [
"f1",
"f2",
"f3"
],
"tests": [
"one",
"two",
"three"
]
}