Facing an issue with a simple requirement.
- Make a CURL request and store in a variable
- Parse JSON from variable using JQ and output into CSV file.
When I try to do it, I am getting extra leading space in the file from the second line for some reason. Is there a way to configure jq to not do this ?
Code :
output=`curl -v -k -H 'Content-Type: application/json' '<<URL>>' -d '<<PARAMS>>'`
csvOutput=`echo $output | jq --raw-output '.result .items[]|[.name,.key,.created,.updated]|@csv'`
echo $csvOutput >> out.csv
The following code (all in one line) does not have the issue. But I need the curl output in a variable to perform some checks ,so this option does not help.
echo "$(curl -v -k -H 'Content-Type: application/json' '<<URL>>' -d '<<PARAMS>>" | jq -r '.result .items[]|[.name,.key,.created,.updated]|@csv' >> out.csv
Strangely, when I read the json output (taken from curl response) from a file and then apply jq filters, I don't see the issue.
cat response.json | jq -r '.result .items[]|[.name,.key,.created,.updated]|@csv' > out.csv
response.json (formatted) :
{
"result": {
"total_items": 22,
"total_pages": 1,
"items_per_page": 1000,
"current_page": 1,
"items": [
{
"key": "1",
"name": "Name 1",
"created": "2016-12-20T08:51:13Z",
"updated": "2016-12-20T09:29:08Z"
},
{
"key": "2",
"name": "Name 2",
"created": "2016-12-20T08:51:13Z",
"updated": "2016-12-20T09:29:08Z"
},
{
"key": "3",
"name": "Name 3",
"created": "2016-12-20T08:51:13Z",
"updated": "2016-12-20T09:29:08Z"
}
]
},
"id": 1
}