Linux Mint 20.2
Here report.empty.json
{
"total": 0,
"project": "",
"Severity": [],
"issues": []
}
I want to set value = 500 (int value) to "total" and "MY_PROJECT".
To do this I use tool "jq"
Here by bash script file:
#!/bin/bash
readonly PROJECT_KEY=MY_PROJECT
readonly PAGE_SIZE=500
jq --arg totalArg "$PAGE_SIZE" '.total = $totalArg' report.empty.json > report.json
jq --arg projectKey "${PROJECT_KEY}" '.project = $projectKey' report.empty.json > report.json
echo "Done"
But it set only key project. The key total is not changed.
Content of file report.json
{
"total": 0,
"project": "MY_PROJECT",
"Severity": [],
"issues": []
}
But I need to update BOTH KEYS.
The result must be:
{
"total": 500,
"project": "MY_PROJECT",
"Severity": [],
"issues": []
}
>Solution :
The second command reads from report.empty.json instead of the already-modified report.json.
You could chain the jq
jq --arg totalArg "$PAGE_SIZE" '.total = $totalArg' report.empty.json |
jq --arg projectKey "${PROJECT_KEY}" '.project = $projectKey' >report.json
But a better solution is to use just use one command.
jq --arg totalArg "$PAGE_SIZE" --arg projectKey "$PROJECT_KEY" '
.total = $totalArg | .project = $projectKey
' report.empty.json >report.json