42

In python you can produce JSON with keys in sorted order by doing

import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4, separators=(',', ': '))

I have not found a similar option in Go. Any ideas how I can achieve similar behavior in go?

sheki
  • 8,991
  • 13
  • 50
  • 69
  • can you please post the solution you used ? I tried using `NewEncoder(...).Encode(structInstance)` but the output json keys are not sorted. – coding_idiot Jun 04 '17 at 04:41

2 Answers2

90

The json package always orders keys when marshalling. Specifically:

  • Maps have their keys sorted lexicographically

  • Structs keys are marshalled in the order defined in the struct

The implementation lives here ATM:

Gustavo Niemeyer
  • 22,007
  • 5
  • 57
  • 46
  • 2
    Noice. I wonder why the encoding/json documentation doesn't mention this crucial property. – Hannes Landeholm Oct 17 '15 at 20:52
  • 6
    Now it is documented : https://golang.org/pkg/encoding/json/#Marshal : Map values encode as JSON objects. The map's key type must either be a string, an integer type, or implement encoding.TextMarshaler. The map keys are sorted and used as JSON object keys by applying the following rules, subject to the UTF-8 coercion described for string values above – programaths Apr 28 '17 at 08:24
  • 11
    What about the struct keys? Is the order documented? – updogliu Jun 08 '17 at 00:41
  • 1
    Radik provided a great answer below. To get the struct keys sorted, you either need to do use his `JsonRemarshal` function (which will incur additional time penalty), or write your own implementation of `Marshal` using reflection. – Victor K. May 16 '21 at 17:33
12

Gustavo Niemeyer gave great answer, just a small handy snippet I use to validate and reorder/normalize []byte representation of json when required

func JSONRemarshal(bytes []byte) ([]byte, error) {
    var ifce interface{}
    err := json.Unmarshal(bytes, &ifce)
    if err != nil {
        return nil, err
    }
    return json.Marshal(ifce)
}
maxm
  • 3,412
  • 1
  • 19
  • 27
Radek 'Goblin' Pieczonka
  • 21,554
  • 7
  • 52
  • 48
  • Does not order elements in json array. I'm using Go 1.19.5. – Elad Tabak Apr 04 '23 at 12:23
  • 3
    and it shouldn't, slices (arrays) in both json and go have particular order, while maps are indexed. In fact, you can say that order of items in array *is* data. The original question is about "keys" hence maps. Reordering an array would be considered a bug. – Radek 'Goblin' Pieczonka Apr 06 '23 at 20:03