I want to clear all values from "dependencies
" by using jq
:
json_string="$(cat <<'END'
{
"name": "hello",
"dependencies": {
"progress": "^2.0.0",
"tar": "^6.2.1"
}
}
END
)"
Here is the expected output:
{
"name": "hello",
"dependencies": {
"progress": "",
"tar": ""
}
}
I have tried but not work:
# output="$(jq ".dependencies =(.dependencies | keys)" <<< "$json_string")"
# echo "output is: $output"
output is: {
"name": "hello",
"dependencies": [
"progress",
"tar"
]
}
It only converts the object to an array, but I want it still as object and only clear all values inside all keys.
What's wrong in my jq
command and how to fix?
You can access the items of an object (here: .dependencies
) by iterating over it using .[]
. Thus, all you need is to set them to ""
(no need for |=
, with_entries
, or map_values
):
jq '.dependencies[] = ""' <<< "$json_string"
{
"name": "hello",
"dependencies": {
"progress": "",
"tar": ""
}
}
I want to clear all values
Note: To "clear" the values, you may want to consider using null
instead of ""
.