Home > Mobile >  Can't set different json values with different values
Can't set different json values with different values

Time:11-11

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": []
}

CodePudding user response:

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

CodePudding user response:

My proposal for How to populate JSON values, using jq

Thinking about How to process arrays using jq, here is my modified version of your script. (Of course, you could keep empty.json out of script)...

#!/bin/bash

declare -r  projectKey=MY_PROJECT
declare -ir pageSize=500

declare -a issueList=()
declare -i issueCnt=0
declare issueStr='' jqCmd='.project = $projArg | .total = $totArg | .issues=[ '
declare promptMessage='Enter issue (or [return] if none): '
while read -rp "$promptMessage" issue && [ "$issue" ];do
    promptMessage='Enter next issue (or [return] if no more): '
    issueCnt =1
    issueList =(--arg is$issueCnt "$issue")
    issueStr ="\$is$issueCnt, "
done
jqCmd ="${issueStr%, } ]"

jq --arg totArg "$pageSize" --arg projArg "$projectKey" \
   "${issueList[@]}" "( $jqCmd )" <<-EoEmptyJson
{
    "total": 0,
    "project": "",
    "Severity": [],
    "issues": []
}
EoEmptyJson

Sample run (I want to add two issues):

./reportJson
Enter issue (or [return] if none): Foo
Enter next issue (or [return] if no more): Bar Baz
Enter next issue (or [return] if no more): 
{
  "total": "500",
  "project": "MY_PROJECT",
  "Severity": [],
  "issues": [
    "Foo",
    "Bar Baz"
  ]
}

CodePudding user response:

No answer (so far) accounts for the requirement that total be of type int. This can be accomplished by using --argjson instead of --arg. Here's my two cents:

jq --argjson total 500 --arg project "MY_PROJECT" '.   {$total, $project}' report.json
{
  "total": 500,
  "project": "MY_PROJECT",
  "Severity": [],
  "issues": []
}
  • Related