Preserving order of fields when parsing
See original GitHub issueI understand that Object
in JS has no concept of order, but Map
does. Since the keys in config yaml file are listed in certain order, there are times when that order matters (i.e. if the yaml file is a set of instructions rather than config). Is there a way to tell the parser to preserve that order and store the data in Map
instead of Object
? I was hoping for something like a flag/option to safeLoad
, but did not see one.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to maintain the order of keys while parsing JSON in groovy
Lets say you have an object Fields that you are converting to json using groovy. you would normally do this new JsonBuilder(obj).toString().
Read more >Preserving JSON object keys order, in JavaScript, Python, and ...
Python has standard library support of JSON handling, so if you have a string of JSON input, the parsing is also easy:
Read more >How to preserve order with JSONObject? - CodeRanch
An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value...
Read more >Swift Dictionary order does not ma… | Apple Developer Forums
I'm working on parsing a JSON file into a dictionary: // JSON Payload File { "orderPlaced": "done", "prep": "pending", "shipped" : "pending" }....
Read more >Jackson JSON - Using @JsonPropertyOrder annotation to ...
@JsonPropertyOrder#alphabetic attribute can be used to order elements alphabetically. Here's the example: @JsonPropertyOrder(alphabetic = true) ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Please, just provide one example from real world, as been requested. This repo is not for discussion about JS, and there are no plans to add maps just for fun.
It’s not so much that it’s not preserved, but that its preservation is a side-effect of current implementation on a particular platform. It’s not guaranteed to be preserved because Object has no concept of order: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order. If Object.keys/entries/values() implementation were to change tomorrow in node/Firefox/Chrome, or is implemented using a different algorithm in another JS environment, the order may come out differently. This could result from something as simple as
Object.keys()
deciding to sort results by key’s hash value.It is one of the main reasons Map (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) was added into the spec, which works the same way but guarantees order. I understand that the logic within js-yaml already scans keys in the same order as they are listed in the yaml file, but once that data goes into
{}
, js-yaml can’t guarantee that the underlying JS engine won’t do something funky with it (i.e. WebAssembly integration/optimization).Looking under the hood (and sorry if I’m wrong), it looks like js-yaml uses traditional
Object
(https://github.com/nodeca/js-yaml/blob/master/lib/js-yaml/dumper.js#L692) to represent the underlying node structure. If there was a flag to switch the underlying structure to aMap
(with all other things being the same), then the user could guarantee order on any version of JS.