title: How to compare JSON with varying order?
body: <p>I'm attempting to implement testing with golden files, however, the JSON my function generates varies in order but maintains the same values. I've implemented the comparison method used here:</p>
  
  <p><a href="https://stackoverflow.com/questions/32408890/how-to-compare-two-json-requests">How to compare two JSON requests?</a></p>
  
  <p>But it's order dependent. And as stated <a href="https://github.com/golang/go/issues/6244" rel="nofollow noreferrer">here</a> by brad:</p>
  
  <blockquote>
    <p>JSON objects are unordered, just like Go maps.  If
    you're depending on the order that a specific implementation serializes your JSON
    objects in, you have a bug.</p>
  </blockquote>
  
  <p>I've written some sample code that simulated my predicament:</p>
  
  <pre><code>package main
  
  import (
      "bufio"
      "encoding/json"
      "fmt"
      "io/ioutil"
      "math/rand"
      "os"
      "reflect"
      "time"
  )
  
  type example struct {
      Name     string
      Earnings float64
  }
  
  func main() {
      slice := GetSlice()
      gfile, err := ioutil.ReadFile("testdata/example.golden")
      if err != nil {
          fmt.Println(err)
          fmt.Println("Failed reading golden file")
      }
  
      testJSON, err := json.Marshal(slice)
      if err != nil {
          fmt.Println(err)
          fmt.Println("Error marshalling slice")
      }
  
      equal, err := JSONBytesEqual(gfile, testJSON)
      if err != nil {
          fmt.Println(err)
          fmt.Println("Error comparing JSON")
      }
  
      if !equal {
          fmt.Println("Restults don't match JSON")
      } else {
          fmt.Println("Success!")
      }
  }
  
  func GetSlice() []example {
      t := []example{
          example{"Penny", 50.0},
          example{"Sheldon", 70.0},
          example{"Raj", 20.0},
          example{"Bernadette", 200.0},
          example{"Amy", 250.0},
          example{"Howard", 1.0}}
      rand.Seed(time.Now().UnixNano())
      rand.Shuffle(len(t), func(i, j int) { t[i], t[j] = t[j], t[i] })
      return t
  }
  
  func JSONBytesEqual(a, b []byte) (bool, error) {
      var j, j2 interface{}
      if err := json.Unmarshal(a, &amp;j); err != nil {
          return false, err
      }
      if err := json.Unmarshal(b, &amp;j2); err != nil {
          return false, err
      }
      return reflect.DeepEqual(j2, j), nil
  }
  
  func WriteTestSliceToFile(arr []example, filename string) {
      file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  
      if err != nil {
          fmt.Println("failed creating file: %s", err)
      }
  
      datawriter := bufio.NewWriter(file)
      marshalledStruct, err := json.Marshal(arr)
      if err != nil {
          fmt.Println("Error marshalling json")
          fmt.Println(err)
      }
      _, err = datawriter.Write(marshalledStruct)
      if err != nil {
          fmt.Println("Error writing to file")
          fmt.Println(err)
      }
  
      datawriter.Flush()
      file.Close()
  }
  </code></pre>