yq/pkg/yqlib/doc/usage
ChrisJr404 2861815f71
fix(json): preserve floats with trailing zero when encoding YAML to JSON (#2701)
YAML scalars tagged `!!float` were round-tripped through `float64` and
re-serialized by Go's JSON encoder, which strips the decimal part of
whole-number floats. As a result, `50.0` came out as `50` and a
sequence like `[50.0, 95.0, 99.0, 99.9]` became `[50,95,99,99.9]`,
turning a uniform array of floats into a mixed int/float array that
downstream consumers (Horreum, JSON Schema validators, jq, etc.)
reject.

The JSON spec does not distinguish ints from floats, but every common
JSON library (Go's `encoding/json`, Python's `json`, jq) preserves the
fractional form of values that came in as floats. yq's YAML decoder
already parses these as `!!float` with the original text intact, so we
can emit them verbatim instead of round-tripping.

`MarshalJSON` for `ScalarNode` now special-cases `!!float`:
- if `Value` is already a JSON-shaped number literal containing a `.`
  or exponent, emit it verbatim (e.g. `50.0`, `99.9`, `1.5e-3`, `-7.0`);
- if `Value` is an integer-shaped string tagged `!!float` (e.g.
  `!!float 5`), format the parsed float and append `.0` so it stays a
  JSON number with a fractional part;
- otherwise (empty value, parse error, or non-finite result), fall back
  to the existing encoding path so behaviour for `.inf` / `.nan` and
  anything unusual is unchanged.

`!!int` nodes still encode as JSON integers.

Closes #2683

Signed-off-by: ChrisJr404 <chris@hacknow.com>
2026-05-14 20:00:34 +10:00
..
headers feat: K8S KYAML output format support (#2560) 2026-01-01 15:14:53 +11:00
base64.md Strip whitespace when decoding base64 #2507 2025-11-15 14:11:55 +11:00
convert.md fix(json): preserve floats with trailing zero when encoding YAML to JSON (#2701) 2026-05-14 20:00:34 +10:00
csv-tsv.md Added new CSV option to turn off auto-parsing #1947 2024-02-15 13:11:53 +11:00
formatting-expressions.md Adding shebang documentation 2024-02-16 11:03:35 +11:00
hcl.md spelling 2025-12-20 09:55:29 +11:00
kyaml.md feat: K8S KYAML output format support (#2560) 2026-01-01 15:14:53 +11:00
lua.md Lua output fixes (#1811) 2023-10-12 11:25:26 +11:00
properties.md Update docs and tests for --properties-separator flag 2025-01-22 09:53:51 +11:00
recipes.md Added prune recipe 2024-02-21 14:50:55 +11:00
shellvariables.md Add --shell-key-separator flag for customizable shell output format 2025-11-07 20:02:58 +11:00
toml.md TOML encoder: prefer readable table sections over inline tables (#2649) 2026-04-12 18:36:43 +10:00
xml.md Fixed comment parsing, added generated docs 2025-12-08 20:30:47 +11:00