DEV Community

Discussion on: The secret power of JSON stringify

Collapse
 
sidvishnoi profile image
Sid Vishnoi • Edited

From tc39.es/ecma262/#sec-json.stringify :

  1. 4.b.ii: If Type(replacer) is [Array]:
  2. 4.b.ii.g: If item is not undefined and item is not currently an element of PropertyList, then Append item to the end of PropertyList.

and later tc39.es/ecma262/#sec-serializejson... :

  1. 5.a: Let K be state.[[PropertyList]].
  2. 8: For each element P of K, do:
  3. 8.v: Append member to partial.
Thread Thread
 
getclibu profile image
Neville Franks

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.

Thread Thread
 
sidvishnoi profile image
Sid Vishnoi

Updated links. DEV included trailing : in links, so they broke.

Thread Thread
 
getclibu profile image
Neville Franks

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:

    var allKeys = [];
    JSON.stringify( json, function( key, value ){ allKeys.push( key ); return value; } )
    allKeys.sort();
Enter fullscreen mode Exit fullscreen mode

which is from: stackoverflow.com/a/53593328/91300