0

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

1
  • 2
    BTW, ideally, all locally-defined variables should have at least one lower-case character: all-caps variable names are used by variables that change the behavior of the shell itself or other POSIX-defined tools. See the relevant standard document at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…, keeping in mind that setting a shell variable overwrites any like-named environment variable; so the only way to avoid overwriting environment variables in reserved space is to also keep regular shell variables out of that space as well. Commented Nov 17, 2021 at 13:31

1 Answer 1

3

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"
Sign up to request clarification or add additional context in comments.

7 Comments

Not sure you even need the temporary file; why not pass slurpfile a process substitution?
(also, you're using double quotes in the second code block when you want $resultIssues to be a jq variable rather than a bash variable -- that should be single quotes -- and there are missing double quotes around other shell expansions; safer to add them to avoid surprising behavior when variables don't have the values they should)
Also, what's the point to < <(echo $(curl ...) | jq) instead of just <(curl ... | jq)?
All good points, thanks. I did not touch the curl invocation in the first rewrite.
@Botje Yes, with temp files no error "Argument list too long". And approach with temp files is VERY FAST. This approach is better.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.