Skip to content

YAML-to-JSON conversion drops .0 from whole-number floats (50.0 → 50) #2683

@dustinblack

Description

@dustinblack

Description

When converting YAML to JSON with -o json, whole-number float values like 50.0 are output as integers (50). This loses type information that YAML explicitly preserves via the !!float tag.

Reproduction

# test.yaml
percentiles:
  - 50.0
  - 95.0
  - 99.0
  - 99.9
$ yq -o json '.' test.yaml

Expected output

{
  "percentiles": [
    50.0,
    95.0,
    99.0,
    99.9
  ]
}

Actual output

{
  "percentiles": [
    50,
    95,
    99,
    99.9
  ]
}

Impact

This creates mixed-type lists in JSON (3 integers + 1 float) from what was a uniform float list in YAML. Downstream consumers that enforce uniform list types reject the output. For example, the Horreum performance data store rejects payloads containing mixed int/float lists.

In our case, YAML output from the Arcaflow engine correctly contains 50.0, but post-processing with yq -o json truncates these to 50, requiring a Python workaround to re-coerce them back to floats.

Environment

  • yq version: v4.44.6 and v4.45.4 (tested both, same behavior)
  • OS: Linux (aarch64)

Notes

While JSON's number type doesn't formally distinguish integers from floats, most JSON encoders (including Go's encoding/json, Python's json, and jq) do preserve 50.0 as a float in output. YAML explicitly tags these values as !!float, so the type information is available to yq during conversion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions