I wan to to add json items to the end of current array "issues".
I try this in bash:
#!/bin/bash
currentReportContent=`cat $FILE_REPORT`
for (( pageNumber=1; pageNumber<=$COUNT_PAGES; pageNumber ))
do
read -r currentIssuesArr < <(echo $(curl -s -XGET $BASE_URL/issues/search\?componentKeys\=$PROJECT_KEY\&p\=$pageNumber\&ps\=$PAGE_SIZE | jq '.issues'))
echo "before_currentIssuesArr = $currentIssuesArr"
# remove first [ and last ]
resultIssues=${currentIssuesArr:1:${#currentIssuesArr}-2}
echo "resultIssues = $resultIssues"
currentReportContent=$(jq -r ".issues[.issues| length] |= . $resultIssues" <<< $currentReportContent)
echo "$currentReportContent" > "$FILE_REPORT"
done
Here output
before_currentIssuesArr = [ { "key": "AX0h7PXHENszTAxEiB7V", "rule": "java:S1871", "severity": "MAJOR", "component": "myProject", "project": "some_component", "line": 390, "hash": "129a49831e0f7ba156e0f3e4e4508a8c", "textRange": { "startLine": 390, "endLine": 393, "startOffset": 2, "endOffset": 9 }, "flows": [ { "locations": [ { "component": "myProject/server-services/services-cfg/cfg-impl/src/", "textRange": { "startLine": 386, "endLine": 389, "startOffset": 2, "endOffset": 9 }, "msg": "Original" } ] } ], "status": "OPEN", "message": "This case's code block is the same as the block for the case on line 386.", "effort": "10min", "debt": "10min", "author": "eugeniur", "tags": [ "design", "suspicious" ], "creationDate": "2021-11-15T06:40:16 0200", "updateDate": "2021-11-15T06:40:16 0200", "type": "CODE_SMELL", "scope": "MAIN" }, { "key": "AX0h7PXvENszTAxEiB7W", "rule": "java:S122", "severity": "MINOR", "component": "myProject/server-services", "project": "some_component", "line": 238, "hash": "e88a41c67f42388448824393203574dd", "textRange": { "startLine": 238, "endLine": 238, "startOffset": 0, "endOffset": 87 }, "flows": [], "status": "OPEN", "message": "At most one statement is allowed per line, but 2 statements were found on this line.", "effort": "1min", "debt": "1min", "author": "eugeniur", "tags": [ "convention" ], "creationDate": "2021-11-15T06:40:16 0200", "updateDate": "2021-11-15T06:40:16 0200", "type": "CODE_SMELL", "scope": "MAIN" } ]
resultIssues = { "key": "AX0h7PXHENszTAxEiB7V", "rule": "java:S1871", "severity": "MAJOR", "component": "myProject/server-services/services-cfg/cfg-impl/src/", "project": "some_component", "line": 390, "hash": "129a49831e0f7ba156e0f3e4e4508a8c", "textRange": { "startLine": 390, "endLine": 393, "startOffset": 2, "endOffset": 9 }, "flows": [ { "locations": [ { "component": "myProject/server-services/services-cfg/cfg-impl/src/", "textRange": { "startLine": 386, "endLine": 389, "startOffset": 2, "endOffset": 9 }, "msg": "Original" } ] } ], "status": "OPEN", "message": "This case's code block is the same as the block for the case on line 386.", "effort": "10min", "debt": "10min", "author": "eugeniur", "tags": [ "design", "suspicious" ], "creationDate": "2021-11-15T06:40:16 0200", "updateDate": "2021-11-15T06:40:16 0200", "type": "CODE_SMELL", "scope": "MAIN" }, { "key": "AX0h7PXvENszTAxEiB7W", "rule": "java:S122", "severity": "MINOR", "component": "myProject/server-service", "project": "some_component", "line": 238, "hash": "e88a41c67f42388448824393203574dd", "textRange": { "startLine": 238, "endLine": 238, "startOffset": 0, "endOffset": 87 }, "flows": [], "status": "OPEN", "message": "At most one statement is allowed per line, but 2 statements were found on this line.", "effort": "1min", "debt": "1min", "author": "eugeniur", "tags": [ "convention" ], "creationDate": "2021-11-15T06:40:16 0200", "updateDate": "2021-11-15T06:40:16 0200", "type": "CODE_SMELL", "scope": "MAIN" }
As you can see it successfully removes the first [
and last ]
.
Nice.
Now I try to add resultIssues to the end of array issues.
But here output in the file:
"issues": [
{
"key": "AX0h7PXHENszTAxEiB7V",
"rule": "java:S1871",
"severity": "MAJOR",
"component": "myProject",
"project": "some_project",
"line": 390,
"hash": "129a49831e0f7ba156e0f3e4e4508a8c",
"textRange": {
"startLine": 390,
"endLine": 393,
"startOffset": 2,
"endOffset": 9
},
"flows": [
{
"locations": [
{
"component": "myProject",
"textRange": {
"startLine": 386,
"endLine": 389,
"startOffset": 2,
"endOffset": 9
},
"msg": "Original"
}
]
}
],
"status": "OPEN",
"message": "This case's code block is the same as the block for the case on line 386.",
"effort": "10min",
"debt": "10min",
"author": "eugeniur",
"tags": [
"design",
"suspicious"
],
"creationDate": "2021-11-15T06:40:16 0200",
"updateDate": "2021-11-15T06:40:16 0200",
"type": "CODE_SMELL",
"scope": "MAIN"
}
]
}
{
"key": "AX0h7PXvENszTAxEiB7W",
"rule": "java:S122",
"severity": "MINOR",
"component": "some_",
"project": "some_project",
"line": 238,
"hash": "e88a41c67f42388448824393203574dd",
"textRange": {
"startLine": 238,
"endLine": 238,
"startOffset": 0,
"endOffset": 87
},
"flows": [],
"status": "OPEN",
"message": "At most one statement is allowed per line, but 2 statements were found on this line.",
"effort": "1min",
"debt": "1min",
"author": "eugeniur",
"tags": [
"convention"
],
"creationDate": "2021-11-15T06:40:16 0200",
"updateDate": "2021-11-15T06:40:16 0200",
"type": "CODE_SMELL",
"scope": "MAIN"
}
Why some json items out of array "issues"?
CodePudding user response:
Why are you mangling JSON with sed if you already have jq?
I would simplify the loop to:
currentReportContent=`cat "$FILE_REPORT"`
for (( pageNumber=1; pageNumber<=$COUNT_PAGES; pageNumber ))
do
read -r resultIssues < <(curl -s -XGET "$BASE_URL/issues/search?componentKeys=$PROJECT_KEY&p=$pageNumber&ps=$PAGE_SIZE" | jq '.issues')
currentReportContent=$(jq --argjson resultIssues "$resultIssues" '.issues |= . $resultIssues' <<< "$currentReportContent")
echo "$currentReportContent" > "$FILE_REPORT"
done
and if you don't mind a few temporary files it becomes massively simpler yet:
allPages=$(mktemp allPages.XXXXXX)
for (( pageNumber=1; pageNumber<=$COUNT_PAGES; pageNumber ))
do
curl -s -XGET "$BASE_URL/issues/search?componentKeys=$PROJECT_KEY&p=$pageNumber&ps=$PAGE_SIZE" | jq '.issues[]' >> $allPages
done
jq --slurpfile resultIssues $allPages '.issues |= . $resultIssues' "$FILE_REPORT" | sponge "$FILE_REPORT"