Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to escape JSON in YAML

Hiren May 14, 2018

Following command works on bash shell but YAML parser (bitbucket-pipelines.yaml) reports it as invalid syntax

- export TASK_VERSION=$(aws ecs register-task-definition --family MyTask --container-definitions "[{\"name\":\"MyContainer\",\"image\":\"1234567.dkr.ecr.us-west-1.amazonaws.com/ecs-repository:1.0.13\",\"cpu\":50,\"portMappings\": [{\"hostPort\": 80,\"containerPort\": 80,\"protocol\":\"tcp\"}],\"memory\":300}]" | jq --raw-output '.taskDefinition.revision')

 

Value of --container-definitions is a JSON string and YAML parser doesn't like JSON.Even though I have escaped all double quotes using \ char, I think [ and { are throwing it off.

Any ideas?

1 answer

0 votes
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 16, 2018

Hi Hiren,

The issue you're encountering is due to the YAML language spec. ": " (specifically with a space) will turn the command string into a map instead. You can use quotes around the command to ensure it's a string. Though this may still have some issues.

This will fix the parsing issues. But may still fail in your build (I haven't checked it).

- "export TASK_VERSION=$(aws ecs register-task-definition --family MyTask --container-definitions \"[{\"name\":\"MyContainer\",\"image\":\"1234567.dkr.ecr.us-west-1.amazonaws.com/ecs-repository:1.0.13\",\"cpu\":50,\"portMappings\": [{\"hostPort\": 80,\"containerPort\": 80,\"protocol\":\"tcp\"}],\"memory\":300}]\" | jq --raw-output '.taskDefinition.revision')"

Alternatively, you can place all of this into a script file and call it directly from your build (as the command is quite complex, so it may be appropriate to abstract it anyways.

Thanks,

Phil

Hiren August 20, 2018

Thanks. I was able to resolve this by using block comment 

- | and put entire command one line below

 

- |

export TASK_VERSION=$(aws ecs register-task-definition --family EventPlannerTask-STG --container-definitions "[{\"name\":\"EventPlannerContainer\",\"image\":\"$IMAGE_NAME\",\"cpu\":50,\"environment\":[{\"name\":\"ASPNETCORE_ENVIRONMENT\",\"value\":\"Staging\"}],\"portMappings\":[{\"hostPort\":80,\"containerPort\":80,\"protocol\":\"tcp\"}],\"memory\":300}]" | jq --raw-output '.taskDefinition.revision')

Like Alejandro Belleza likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events