Split a string field into an array in jq?
You're making it a lot more complicated than it is. Just use map()
and |=
:
jq 'map(.tags |= split(" "))' file.json
Edit:
If you want to handle entries without tags
:
jq 'map(try(.tags |= split(" ")))' file.json
Alternatively, if you want to keep unchanged all entries without tags
:
jq 'map(try(.tags |= split(" ")) // .)' file.json
Result:
[
{
"tags": [
"tagA",
"tag-B",
"tagC"
],
"title": "Some Title"
},
{
"tags": [
"tagA",
"tagC"
],
"title": "Some Title 2"
}
]