title: How can we read a json file as json object in golang
body: <p>I have a JSON file stored on the local machine. I need to read it in a variable and loop through it to fetch the JSON object values. If I use the Marshal command after reading the file using the ioutil.Readfile method, it gives some numbers as an output. These are my few failed attempts,</p>
  
  <p>Attempt 1: </p>
  
  <pre><code>plan, _ := ioutil.ReadFile(filename) // filename is the JSON file to read
  var data interface{}
  err := json.Unmarshal(plan, data)
  if err != nil {
          log.Error("Cannot unmarshal the json ", err)
        }
  fmt.Println(data)
  </code></pre>
  
  <p>It gave me following error,</p>
  
  <pre><code>time="2016-12-13T22:13:05-08:00" level=error msg="Cannot unmarshal the json json: Unmarshal(nil)"
  &lt;nil&gt;
  </code></pre>
  
  <p>Attempt 2: I tried to store the JSON values in a struct and then using MarshalIndent</p>
  
  <pre><code>generatePlan, _ := json.MarshalIndent(plan, "", " ") // plan is a pointer to a struct
  fmt.Println(string(generatePlan))
  </code></pre>
  
  <p>It give me the output as string. But if I cast the output to string then I won't be able to loop it as JSON object.</p>
  
  <p>How can we read a JSON file as JSON object in golang?  Is it possible to do that?
  Any help is appreciated. Thanks in advance!</p>