kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.57k stars 82 forks source link

json output converts numbers to strings #119

Open dmivankov opened 3 years ago

dmivankov commented 3 years ago
# roundtrip works
$ echo "a: 1e+9" | yq -Y .
a: 1e+9

# json output quotes the number
$ echo "a: 1e+9" | yq  .
{
  "a": "1e+9"
}

# json output can't be used for roundtrip
$ echo "a: 1e+9" | yq  . | yq -Y .
a: "1e+9"

# same output with string in input as with number
$ echo "a: \"1e+9\"" | yq  . 
{
  "a": "1e+9"
}

"Another yq" (https://github.com/mikefarah/yq) outputs numbers as json numbers

$ echo "a: 1e+9" | yq eval . -j - 
{
  "a": 1000000000
}

Would be nice to be able to do the roundtrip with intermediate json format too, or in other words make json conversion not lossy.

kislyuk commented 3 years ago

This is a problem in the underlying YAML parser, PyYAML: https://github.com/yaml/pyyaml/issues/173. It seems the workaround is to patch the YAML parser's configuration with better regular expressions for floating point: https://stackoverflow.com/a/30462009/1380386