-1

How can I update a json file using jq in a loop as below ? json file need to have the .queryToken updated and .result array appended using json in curl response body

while true
do
  curl -k --location 'https://api.../queryMore' \
          --user "uname:pswd" \
          --data "$(jq '.queryToken' Process.json)" |
   jq '{
         numberOfResults: .numberOfResults,
         queryToken: .queryToken,
         result: .result
       }'
 #update .queryToken in Process.json and append .result to .result in Process.json
 #break if no .queryToken
done

The file and curl response are identical in structure as in example below

{
    "@type": "QueryResult",
    "queryToken": "<base64text>",
    "result": [
      ...
    ]
}
9
  • What's the type of .result? A string, an array, and object? Have you considered using a proper programming languages like perl/ruby/python instead of a shell? Commented Aug 30, 2024 at 8:50
  • .result is an array of objects. If not jq/bash then fallback is nodejs script Commented Aug 30, 2024 at 9:23
  • An example of what you have and of what you want, please Commented Aug 30, 2024 at 9:25
  • The use case is for the api call as per help.boomi.com/docs/atomsphere/integration/atomsphere%20api/… Commented Aug 30, 2024 at 9:33
  • Make it easy for us to help you. Put the example of what you have and what you want into your question please Commented Aug 30, 2024 at 9:36

1 Answer 1

1

Could be something like:

set -o pipefail
while
  token=$(jq -e .queryToken Process.json) &&

    curl --insecure \
         --fail \
         --no-progress-meter \
         --location \
         --user "uname:pswd" \
         --data "$token" \
         'https://api.../queryMore' |
      jq -c --slurpfile old Process.json '
        . as $new |
        $old | first |
        .numberOfResults += ($new.numberOfResults) |
        .result += ($new.result) |
        .queryToken = ($new.queryToken)' > Process.json.new &&

    mv Process.json.new Process.json
do
  continue
done

(also replacing your -k with its long form --insecure so as not to loose sight of the fact that it is insecure. Also adding --fail for curl to report errors by the HTTP server; I've also moved the URL to the end. Having it after --location was misleading, as it's not an argument to that option; --location is a flag to tell curl to follow redirects¹; you should also consider using safer ways to pass the credentials as passing them on the command line is insecure).


¹ as supplied by the HTTP server in the Location HTTP header. See also the --location-trusted for the --user credentials to also be sent to the target of the redirection (potentially dangerous especially when combined with -k aka --insecure).

1
  • I am still not sure what clarity is required. Anyhow thanks to Stéphane Chazelas for the solution, i can confirm that it works as desired. Commented Oct 1, 2024 at 5:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.