Please Review

Apr 24, 2025

How to sort JSON keys in lexicographic order when encoding data

In order to achieve this goal - which can help with quickly reading through large JSONs or making tests easier - you can use .sortedKeys as the value for the outputFormatting property of JSONEncoder.

For convenience, you can create a small extension :

extension JSONEncoder {
  static func withSortedKeys() -> JSONEncoder {
    let encoder = JSONEncoder()
    encoder.outputFormatting = .sortedKeys
    return encoder
  }
}

And for an even easier reading experience, you can combine it with .prettyPrinted:

encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
Tagged with: