title: AWS CLI returns JSON with control codes making JQ fail
body: <p>I've used <code>jq</code> many times to parse, pick values etc from JSON returned by AWS CLI, e.g. for <code>ec2 describe-instances</code> etc.</p>
  <p>Now I'm using the <a href="https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-docker.html" rel="nofollow noreferrer">dockerized version of AWS CLI v2</a> to get a list of CloudWatch log groups:</p>
  <pre><code>$ alias aws='docker run --rm -it -v ~/.aws:/root/.aws -e AWS_PROFILE -e AWS_REGION amazon/aws-cli'
  $ aws logs describe-log-groups
  
  {
      &quot;logGroups&quot;: [
          {
              ...
          }
      ]
  }
  </code></pre>
  <p>This looks like proper JSON; however when piping this to <code>jq</code>, I get:</p>
  <p><code>parse error: Invalid numeric literal at line 1, column 2</code></p>
  <p>If looking at the JSON returned from <code>aws</code> in a binary editor, or using <code>jq</code>:s <code>inputs</code> feature, I see that it contains a lot of control codes:</p>
  <pre><code>[
    &quot;\u001b[?1h\u001b=\r{\u001b[m\r&quot;,
    &quot;    \&quot;logGroups\&quot;: [\u001b[m\r&quot;,
    &quot;        {\u001b[m\r&quot;,
    &quot;            \&quot;logGroupName\&quot;: \&quot;/aws/lambda/...
  ...
  </code></pre>
  <p>It seems to me that it's the fact that I'm using AWS CLI through docker that causes this, because when using the old fashioned AWS CLI v1 installed using <code>pip</code> it does not happen - but it could also be v2 vs v1 that is the key difference rather than using docker as the environment I guess (I never tried installing v2 natively).</p>
  <p>These control codes, e.g. <code>\u001b[m</code>, look like ANSI codes to control formatting such as bold, colors etc. But AFAIK the AWS CLI does not use colored/ANSI output. Why are they included in the returned JSON? Is there a simple tool to strip them away so that I can continue using the dockerized AWS CLI v2 and pipe the output to <code>jq</code>? I found other answers using complex <code>sed</code> patterns and I thought to myself that there must be a simpler way to do this?</p>
  <p><strong>Edit</strong>: here's a minimal example that shows the control codes using <code>xxd</code>. I deliberately listed log groups with a mismatching filter to get an empty array:</p>
  <pre><code>$ aws logs describe-log-groups --log-group-name-prefix FOO &gt; foo.txt
  $ xxd foo.txt
  00000000: 1b5b 3f31 681b 3d0d 7b1b 5b6d 0d0a 2020  .[?1h.=.{.[m..  
  00000010: 2020 226c 6f67 4772 6f75 7073 223a 205b    &quot;logGroups&quot;: [
  00000020: 5d1b 5b6d 0d0a 7d1b 5b6d 0d0a 0d1b 5b4b  ].[m..}.[m....[K
  00000030: 1b5b 3f31 6c1b 3e                        .[?1l.&gt;
  $ cat foo.txt | jq
  parse error: Invalid numeric literal at line 1, column 2
                                                          $
  </code></pre>
  <p>Same thing displayed with <code>xxd</code> when using non-dockerized AWS CLI v1:</p>
  <pre><code>00000000: 7b0a 2020 2020 226c 6f67 4772 6f75 7073  {.    &quot;logGroups
  00000010: 223a 205b 5d0a 7d0a                      &quot;: [].}.
  </code></pre>