I'm trying to create a JSON data and store it to a file and then feed it to the API call I'm gonna make. However, upon formatting the output of my echo command to make a JSON format a variable is causing disarray in the returned output. Below is my script
cat "${SETTINGS_LIST}" | while read endpoint
do
echo "{\"id\": \"$n\", \"method\": GET, \"url\": \"$endpoint\"}"
let n=n+1
done
then this is the content of SETTING_LIST variable
/accounting-rules
/aging-buckets
/application-rules
/audit-trail-settings
/batch-aliases
/billing-cycle-types
/billing-list-price-bases
/billing-period-starts
/billing-periods
/billing-rules
And the output is returning like this:
"}id":"0","method":"GET","url":"/accounting-rules
"}id":"1","method":"GET","url":"/aging-buckets
"}id":"2","method":"GET","url":"/application-rules
"}id":"3","method":"GET","url":"/audit-trail-settings
"}id":"4","method":"GET","url":"/batch-aliases
"}id":"5","method":"GET","url":"/billing-cycle-types
"}id":"6","method":"GET","url":"/billing-list-price-bases
"}id":"7","method":"GET","url":"/billing-period-starts
"}id":"8","method":"GET","url":"/billing-periods
"}id":"9","method":"GET","url":"/billing-rules
When I removed the endpoint variable and change it to this, for example
echo "{\"id\": \"$n\", \"method\": GET, \"url\": \"/application-rules\"}"
I'm getting the expected output
{"id": "0", "method": GET, "url": "/application-rules"}
{"id": "1", "method": GET, "url": "/application-rules"}
{"id": "2", "method": GET, "url": "/application-rules"}
{"id": "3", "method": GET, "url": "/application-rules"}
{"id": "4", "method": GET, "url": "/application-rules"}
{"id": "5", "method": GET, "url": "/application-rules"}
{"id": "6", "method": GET, "url": "/application-rules"}
{"id": "7", "method": GET, "url": "/application-rules"}
{"id": "8", "method": GET, "url": "/application-rules"}
{"id": "9", "method": GET, "url": "/application-rules"}
Actions I have done:
- Downgraded to older git bash version
- Tried in my Ubuntu WSL but still getting the same error.
- Tried different way of formatting the output to JSON by using JQ and printf command but issue still persist
- The script works fine in different machine with same version of BASH and GIT BASH
Does anybody know the cause of this issue and how this could be possibly resolved?