You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to print out the JSON maps in alphabetical order.
I noticed that the encode function will always return the keys in reverse order. Am I missing something? I'm surprised that an option for alphabetical order hasn't been requested already.
Add more keys and you will hit a threshold at which they're no longer in a discernible order; #21 (comment)
I have also needed this, though, when I was writing some tests that embedded JSON inside another document. I used this function to encode:
@doc""" Encode a map to JSON with all of the keys in alphabetical order (including for objects nested inside this object). """defencode_object_in_alphanumeric_key_order(obj)whenis_map(obj)doaz_keys=obj|>Map.keys|>Enum.sortiodata=["{",Enum.map(az_keys,fnk->v=obj[k][Poison.encode!(k),":",encode_object_in_alphanumeric_key_order(v)]end)|>Enum.intersperse(","),"}"]IO.iodata_to_binary(iodata)enddefencode_object_in_alphanumeric_key_order(obj),do: Poison.encode!(obj)
I would like to print out the JSON maps in alphabetical order.
I noticed that the encode function will always return the keys in reverse order. Am I missing something? I'm surprised that an option for alphabetical order hasn't been requested already.
The text was updated successfully, but these errors were encountered: