Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't set different json values with different values

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading