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

Keep the original order of nodes in the output #8

Open
pspot2 opened this issue Nov 3, 2022 · 0 comments
Open

Keep the original order of nodes in the output #8

pspot2 opened this issue Nov 3, 2022 · 0 comments

Comments

@pspot2
Copy link

pspot2 commented Nov 3, 2022

Hi,

I'm using this functionality primarily as Terraform's yamldecode() function. For scenarios where humans are not supposed to look at the output this works perfectly fine. However, there are also some use-cases where the decoded (JSON) output would be put as an input to an AWS Step Functions state machine, and that's where humans do need to look at that JSON input, meaning that the tree needs to be logically structured for easy reading.

yamldecode() doesn't seem to contain any options/flags that would control its behavior and by default it seems to sort the nodes alphabetically in the output. E.g. the following YAML input:

two:
    node2:
        some: things
        baz: bar
    node1:
        hello: world
        foo: bar

one:
    testB: second
    testA: first

Is decoded as the following JSON:

> yamldecode(file("file.yml"))
{
  "one" = {
    "testA" = "first"
    "testB" = "second"
  }
  "two" = {
    "node1" = {
      "foo" = "bar"
      "hello" = "world"
    }
    "node2" = {
      "baz" = "bar"
      "some" = "things"
    }
  }
}

By comparison, pyyaml produces the required output:

{
  "two": {
    "node2": {
      "some": "things",
      "baz": "bar"
    },
    "node1": {
      "hello": "world",
      "foo": "bar"
    }
  },
  "one": {
    "testB": "second",
    "testA": "first"
  }
}

Would it be possible to either output the original order of nodes as a default behavior or, if it hurts performance too much, make it an optional behavior by setting some flag?

Thanks

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

1 participant