I have a file api_definitions.json:
{
"basePolicy": "<policies><inbound><base /><set-header name="X-Common-Header" exists-action="override"><value>common-value</value></set-header></inbound><backend><base /></backend><outbound><base /></outbound><on-error><base /></on-error></policies>",
"apis": [
{
"resourceGroupName": "my-rg",
"serviceName": "my-apim",
"apiId": "yourAPIId1",
"specificationUrl": "/swagger/api1.yaml",
"endpointPolicy": "<set-header name="X-Custom-Header-1" exists-action="override"><value>value1</value></set-header>"
},
{
"resourceGroupName": "my-rg",
"serviceName": "my-apim",
"apiId": "yourAPIId2",
"specificationUrl": "/swagger/api2.yaml",
"endpointPolicy": "<set-header name="X-Custom-Header-2" exists-action="override"><value>value2</value></set-header>"
}
]
}
I am trying to parse this file with jq:
API_DEFINITIONS_FILE="api_definitions.json"
api_definitions=$(cat "$API_DEFINITIONS_FILE")
base_policy=$(echo "$api_definitions" | jq -r '.basePolicy')
echo "Api Definitions: "$api_definitions""
for api in $(echo "$api_definitions" | jq -c '.apis[]'); do
echo "API: $api"
echo "get resourceGroupName"
resourceGroupName=$(echo "$api" | jq -r '.resourceGroupName')
done
when running this script in ado I get:
Api Definitions: { "basePolicy": "<policies><inbound><base /><set-header name=\"X-Common-Header\" exists-action=\"override\"><value>common-value</value></set-header></inbound><backend><base /></backend><outbound><base /></outbound><on-error><base /></on-error></policies>", "apis": [ { "resourceGroupName": "my-rg", "serviceName": "my-apim", "apiId": "yourAPIId1", "specificationUrl": "/swagger/api1.yaml", "endpointPolicy": "<set-header name=\"X-Custom-Header-1\" exists-action=\"override\"><value>value1</value></set-header>" }, { "resourceGroupName": "my-rg", "serviceName": "my-apim", "apiId": "yourAPIId2", "specificationUrl": "/swagger/api2.yaml", "endpointPolicy": "<set-header name=\"X-Custom-Header-2\" exists-action=\"override\"><value>value2</value></set-header>" } ] }
API: {"resourceGroupName":"my-rg","serviceName":"my-apim","apiId":"yourAPIId1","specificationUrl":"/swagger/api1.yaml","endpointPolicy":"<set-header
get resourceGroupName
parse error: Unfinished string at EOF at line 2, column 0
It seems that the space after set-header might be causing the rest of the string to not be parsed properly with jq. I cant figure out what the cause is.