Home > Blockchain >  How to update a field with a new value in dynamic variable using bash?
How to update a field with a new value in dynamic variable using bash?

Time:01-29

How to update a field with a new value in a dynamic variable using bash?

I am storing some curl output in a variable below is the output response in it.

data=$(curl -s --location --request GET "${HOST}/api/v1/envs/projects/${PROJECT_ID}?page=0&pageSize=25" --header "Accept: application/json" --header "Content-Type: application/json" --header "Authorization: Bearer "$token"" | jq -r '.data[]')
echo "$data"

{
  "id": "8aa0809085f46e000185f4c28b7a0b52",
  "createdBy": "8aa0808e82fd01fb0182fd0b05820007",
  "createdDate": "2023-01-27T19:44:28.538 0000",
  "modifiedBy": "8aa0808e82fd01fb0182fd0b05820007",
  "modifiedDate": "2023-01-27T19:45:14.811 0000",
  "version": null,
  "inactive": false,
  "projectId": "8aa0809085f46e000185f4c28b760b51",
  "name": "Master",
  "refId": "Master",
  "description": null,
  "baseUrl": "http://petstore.swagger.io/v1",
}

Now I want to update the baseUrl field in that data variable dynamically and store that update complete JSON object either in same or different variable to be able use it in a different curl request operation.

How can we do that in bash scripting?

CodePudding user response:

You are already using jq to extract part of the data returned from the API. You can extend this jq command to modify the baseURL field on the fly. The following will update baseURL in the first object in the data array, then print it out:

jq -r '.data[0].baseUrl = "http://example.com" | .data[0]'
  • Related