title: Escaping JSON string inside Jenkinsfile shell
body: <p>I am trying to escape a json-string inside a jenkinsfile but it is a little complicated that the examples I have checked online. </p>
  
  <pre><code>sshagent(credentials: ['keypair']) {
              sh """
                  ssh ansible@ansible-server \
                  '
  # vars
  src_env="${params.source_env}"
  dest_env="${params.destination_env}"
  src_prefix="${params.source_s3_prefix}"
  dest_prefix="${params.destination_s3_prefix}"
  region="${region}"
  ch="${ch}"
  aws_account="${aws_account}"
  
  # Run ECS task
  echo "---- Run ECS task ----"
  task_id=$(aws ecs run-task \
    --cluster cluster-\${ch}-\${dest_env} \
    --task-definition \$td_id \
    --region \${region} \
    --overrides {\\\"containerOverrides\\\": [{\\\"name\\\": \\\"s3-sync\\\", \\\"environment\\\": [{\\\"name\\\": \\\"SRC_ENV\\\", \\\"value\\\": \\\"$${src_env}\\\"}, {\\\"name\\\": \\\"DEST_ENV\\\", \\\"value\\\": \\\"$${dest_env}\\\"}, {\\\"name\\\": \\\"SRC_PREFIX\\\", \\\"value\\\": \\\"$${src_prefix}\\\"}, {\\\"name\\\": \\\"DEST_PREFIX\\\", \\\"value\\\": \\\"$${dest_prefix}\\\"}]}], \\\"taskRoleArn\\\":  \\\"arn:aws:iam::$${aws_account}:role/$${ch}-$${dest_env}\\\"} \
    --query 'tasks[0].taskArn' \
    --output text | cut -d'/' -f 2)
  
                  '
              """
  }
  </code></pre>
  
  <p>So, the problem is to escape the following json string:</p>
  
  <pre><code>--overrides {\\\"containerOverrides\\\": [{\\\"name\\\": \\\"s3-sync\\\", \\\"environment\\\": [{\\\"name\\\": \\\"SRC_ENV\\\", \\\"value\\\": \\\"$${src_env}\\\"}, {\\\"name\\\": \\\"DEST_ENV\\\", \\\"value\\\": \\\"$${dest_env}\\\"}, {\\\"name\\\": \\\"SRC_PREFIX\\\", \\\"value\\\": \\\"$${src_prefix}\\\"}, {\\\"name\\\": \\\"DEST_PREFIX\\\", \\\"value\\\": \\\"$${dest_prefix}\\\"}]}], \\\"taskRoleArn\\\":  \\\"arn:aws:iam::$${aws_account}:role/$${ch}-$${dest_env}\\\"} \
  </code></pre>
  
  <p>Any help appreciated? I have tried lots of different ways but nothing seems to work. </p>