- 1 year ago
jq assignment where the path contain space
When using jq
to modify JSON data where the path contains spaces, you can enclose the entire path in square brackets and use double quotes. Here's the general syntax:
bashjq '.["path with spaces"] = new_value' input.json > output.json
Replace "path with spaces"
with the actual path containing spaces and new_value
with the value you want to assign.
For example, let's say you have the following JSON file named data.json
:
json{
"person info": {
"name": "John Doe",
"age": 30
}
}
You want to change the name. You can use jq
as follows:
bashjq '.["person info"]["name"] = "Jane Smith"' data.json > updated_data.json
This will update the name
value to "Jane Smith" in the JSON file.
Keep in mind that jq
creates a new JSON output with the changes applied. If you want to modify the JSON in-place, you can use a temporary file and then replace the original file:
bashjq '.["person info"]["name"] = "Jane Smith"' data.json > tmp.json && mv tmp.json data.json
Remember to back up your data before making such changes, especially if you're modifying the JSON in-place.