sortJSONKeys recursively sorts all object keys in a JSON byte array by unmarshaling to map[string]any and remarshaling. Go's JSON encoder automatically sorts map keys alphabetically.
(jsonData []byte)
| 92 | // unmarshaling to map[string]any and remarshaling. Go's JSON encoder |
| 93 | // automatically sorts map keys alphabetically. |
| 94 | func sortJSONKeys(jsonData []byte) ([]byte, error) { |
| 95 | var data any |
| 96 | if err := json.Unmarshal(jsonData, &data); err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | |
| 100 | return json.MarshalIndent(data, "", " ") |
| 101 | } |
no outgoing calls