I'd been looking everywhere to find a simple way to get JSON.stringify() to output items in a specific order. You post provided exactly what I needed to do what I wanted.
Unfortunately upon reading the JSON.stringify() docs I can't find any information on using a replacer array to set keys order. Only info about whitelists. Can you point to any docs on this?
Some other info I found that may be if interest to others: "sort object properties and JSON.stringify" - tfzx.net/article/499097.html
I'd been looking everywhere to find a simple way to get JSON.stringify() to output items in a specific order. You post provided exactly what I needed to do what I wanted.
Unfortunately upon reading the
JSON.stringify()
docs I can't find any information on using a replacer array to set keys order. Only info about whitelists. Can you point to any docs on this?Some other info I found that may be if interest to others: "sort object properties and JSON.stringify" - tfzx.net/article/499097.html
Thanks so much for your post.
From tc39.es/ecma262/#sec-json.stringify :
Type(replacer)
is [Array]:and later tc39.es/ecma262/#sec-serializejson... :
Thanks for that. Pity it is missing from the MDN Doc's. For some reason the links didn't take me to the relevant sections.
Updated links. DEV included trailing
:
in links, so they broke.Thanks, the links now work.
I didn't pick up on the ", but with limitation of maximum object depth 1" issue and I need to handle objects with depth > 1.
After some more searching I found this article which is a collection of code snippets. tfzx.net/article/499097.html
The code that worked for me is:
which is from: stackoverflow.com/a/53593328/91300