Skip to content

Conversation

@jiasli
Copy link
Member

@jiasli jiasli commented Feb 17, 2020

Fix #2924: JSONC is not parsed successfully by json.loads in Python2

On Linux, when the output format is set to jsonc or yamlc whether by changing the ~/.azure/config file or by using --output, there will be ANSI control sequences for coloring like ^[[94m in stdout. If stdout is redirected, downstream commands like jq will fail:

$ az vm list --output jsonc | jq '.[0]'
parse error: Invalid numeric literal at line 3, column 6

$ az vm list --output jsonc > out.txt
$ cat out.txt --show-nonprinting
[
  {
    ^[[94m"additionalCapabilities"^[[39;49;00m: ^[[34mnull^[[39;49;00m,
    ^[[94m"availabilitySet"^[[39;49;00m: ^[[34mnull^[[39;49;00m,

This PR detects whether stdout is a tty. If not, use json or yaml instead, thus allowing the output to be piped to downstream commands:

$ az vm list --output jsonc | jq '.[].name'
"azureclitestlinux"
"azureclitestwin"

$ az vm list --output json > out.txt
$ cat out.txt --show-nonprinting
[
  {
    "additionalCapabilities": null,
    "availabilitySet": null,

ℹ This issue doesn't happen on Windows because ANSI control sequences are removed by colorama.

@jiasli jiasli self-assigned this Feb 17, 2020
@jiasli jiasli added this to the S166 milestone Feb 17, 2020
@jiasli
Copy link
Member Author

jiasli commented Feb 17, 2020

Closing as this should go to https://github.com/microsoft/knack

@jiasli jiasli closed this Feb 17, 2020
@jiasli jiasli deleted the remove-color branch April 16, 2020 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSONC is not parsed successfully by json.loads in Python2.

1 participant