Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keys like "yes" and "on" are read as true #77

Open
slntopp opened this issue Apr 12, 2022 · 6 comments
Open

Keys like "yes" and "on" are read as true #77

slntopp opened this issue Apr 12, 2022 · 6 comments

Comments

@slntopp
Copy link

slntopp commented Apr 12, 2022

template = `
on:
 - RUNNING
`
template, _ = yaml.YAMLToJSON(template)
fmt.Println(string(template))
// => {"true":["DELETED"]}
@s1moe2
Copy link

s1moe2 commented Apr 21, 2022

I was about to open a similar issue. y is also read as true.
Not great but solved by quoting the property: "y"

@slntopp
Copy link
Author

slntopp commented Apr 22, 2022

yeah, I figured too, but unfortunately not a solution in my case :(

@fproulx-boostsecurity
Copy link

We have the same issue. It affects https://github.com/open-policy-agent/opa which uses this library.

@samlown
Copy link

samlown commented May 27, 2022

I can see that would have been very annoying! It's to do with the way yaml.v2 was parsing keys as interface{} instead of string.. That still happens in yaml.v3, but only if all keys are non-strings. Moving to v3 should fix this.

Doesn't look there was much maintenance here, so I've forked this repo and upgraded to v3 if you'd like to test it: https://github.com/invopop/yaml

@slyt
Copy link

slyt commented Nov 25, 2024

Ran into this same issue today:

n: "hello" gets converted to {"false":"hello"}

"n": "hello" gets converted to {"n":"hello"} so quoting your YAML keys is a workaround

@slyt
Copy link

slyt commented Nov 26, 2024

This happens for both keys and values if unquotted y, n, Y, N:

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"github.com/ghodss/yaml"
)

func main() {
	// Read the YAML file.
	yamlFile := "input.yaml"
	yamlData, err := ioutil.ReadFile(yamlFile)
	if err != nil {
		log.Fatalf("Error reading YAML file: %v", err)
	}

	// Convert YAML to JSON.
	jsonData, err := yaml.YAMLToJSON(yamlData)
	if err != nil {
		log.Fatalf("Error converting YAML to JSON: %v", err)
	}

	// Print the resulting JSON.
	fmt.Println(string(jsonData))
}

input.yaml

n: 
  title: N
  type: integer
  default: 1
y:
  nested:
    n:
      nested:
        title: Y
        title_2: N

converts to:

{
  "false": { "default": 1, "title": false, "type": "integer" },
  "true": {
    "nested": { "false": { "nested": { "title": true, "title_2": false } } }
  }
}

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

No branches or pull requests

5 participants