title: merge JSON objects recursively
body: <p>Here's an example of what I need:</p>
  
  <p>Default JSON:</p>
  
  <pre><code>{
      "name": "John",
      "greetings": {
          "first": "hi",
          "second": "hello"
      }
  }
  </code></pre>
  
  <p>merged with the changes:</p>
  
  <pre><code>{
      "name": "Jane",
      "greetings": {
          "first": "hey"
      }
  }
  </code></pre>
  
  <p>should become:</p>
  
  <pre><code>{
      "name": "Jane",
      "greetings": {
          "first": "hey",
          "second": "hello"
      }
  }
  </code></pre>
  
  <p>Here's what I've tried:</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "reflect"
  )
  
  func MergeJSON(defaultJSON, changedJSON string) string {
      var defaultJSONDecoded map[string]interface{}
      defaultJSONUnmarshalErr := json.Unmarshal([]byte(defaultJSON), &amp;defaultJSONDecoded)
      if defaultJSONUnmarshalErr != nil {
          panic("Error unmarshalling first JSON")
      }
      var changedJSONDecoded map[string]interface{}
      changedJSONUnmarshalErr := json.Unmarshal([]byte(changedJSON), &amp;changedJSONDecoded)
      if changedJSONUnmarshalErr != nil {
          panic("Error unmarshalling second JSON")
      }
      for key, _ := range defaultJSONDecoded {
          checkKeyBeforeMerging(key, defaultJSONDecoded[key], changedJSONDecoded[key], changedJSONDecoded)
      }
      mergedJSON, mergedJSONErr := json.Marshal(changedJSONDecoded)
      if mergedJSONErr != nil {
          panic("Error marshalling merging JSON")
      }
      return string(mergedJSON)
  }
  
  func checkKeyBeforeMerging(key string, defaultMap interface{}, changedMap interface{}, finalMap map[string]interface{}) {
      if !reflect.DeepEqual(defaultMap, changedMap) {
          switch defaultMap.(type) {
          case map[string]interface{}:
              //Check that the changed map value doesn't contain this map at all and is nil
              if changedMap == nil {
                  finalMap[key] = defaultMap
              } else if _, ok := changedMap.(map[string]interface{}); ok { //Check that the changed map value is also a map[string]interface
                  defaultMapRef := defaultMap.(map[string]interface{})
                  changedMapRef := changedMap.(map[string]interface{})
                  for newKey, _ := range defaultMapRef {
                      checkKeyBeforeMerging(newKey, defaultMapRef[newKey], changedMapRef[newKey], finalMap)
                  }
              }
          default:
              //Check if the value was set, otherwise set it
              if changedMap == nil {
                  finalMap[key] = defaultMap
              }
          }
      }
  }
  
  func main() {
      defaultJSON := `{"name":"John","greetings":{"first":"hi","second":"hello"}}`
      changedJSON := `{"name":"Jane","greetings":{"first":"hey"}}`
      mergedJSON := MergeJSON(defaultJSON, changedJSON)
      fmt.Println(mergedJSON)
  }
  </code></pre>
  
  <p>The code above returns the following:</p>
  
  <pre><code>{
      "greetings": {
          "first": "hey"
      },
      "name": "Jane",
      "second": "hello"
  }
  </code></pre>
  
  <p>So basically any changes should be applied to the default and return the full JSON. I also need this to work recursively.</p>
  
  <p>How can I fix this? I can see where I went wrong, I'm just not sure how to make it work recursively.</p>
  
  <p>Thanks</p>