DEV Community

Sam
Sam

Posted on

Parse a JSON into struct

This short snippet shows you how to parse a JSON with a generic, random structure, stored in a file.

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

func main() {

    path := "./obj.json"

    jsonFile, err := os.Open(path)
    if err != nil {
        fmt.Println(err)
    }
    defer jsonFile.Close()
    bytes, _ := ioutil.ReadAll(jsonFile)

    var parsed map[string]interface{}
    json.Unmarshal([]byte(bytes), &parsed)
    fmt.Printf("%#v", parsed)

}
Enter fullscreen mode Exit fullscreen mode

To test the code, you can use a json file like this:

obj.json

{
  "prop": "value",
  "students": [
    {
      "name": "John",
      "age": "20",
      "marks": "80",
      "address": {
        "city": "New York",
        "state": "NY"
      }
    },
    {
      "name": "Jane",
      "age": "20",
      "marks": "90",
      "address": {
        "city": "New York",
        "state": "NY"
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

The output for this example would be like this:

map[string]interface {}{"prop":"value", "students":[]interface {}{map[string]interface {}{"address":map[string]interface {}{"city":"New York", "state":"NY"}, "age":"20", "marks":"80", "name":"John"}, map[string]interface {}{"address":map[string]interface {}{"city":"New York", "state":"NY"}, "age":"20", "marks":"90", "name":"Jane"}}}%

map[string]interface {}{"prop":"value", "students":[]interface {}{map[string]interface {}{"address":map[string]interface {}{"city":"New York", "state":"NY"}, "age":"20", "marks":"80", "name":"John"}, map[string]interface {}{"address":map[string]interface {}{"city":"New York", "state":"NY"}, "age":"20", "marks":"90", "name":"Jane"}}}%

Note: if your JSON object has a format you know beforehand, you can use a struct instead of map[string]interface{}


I'm the maintainer of DoTenX, and invite you to take a look at this repository, if you enjoy coding with Golang or learning it:

https://github.com/dotenx/dotenx

Top comments (3)

Collapse
 
crossoverjie profile image
crossoverJie

I have written a JSON parsing libaray that just makes it easier to query a key.

github.com/crossoverJie/xjson

Collapse
 
mohsenkamrani profile image
Sam

Nice! Specially the nested properties and arithmetic operations are interesting.

Collapse
 
limrs12 profile image
home interior

Would you like to arrange a decent quality inside to design the interior of your house? Then contact our company now. Our company is known as a respected company in Bangladesh LIMRS Constructions and Interior is a highly regarded firm, participating in spaces such as Civil Construction, Home Interior Designing, Architecture, and so on. Home interior design company provides excellent quality service at low price in Bangladesh .The best home interior design service in Bangladesh. Home Interior Design in Bangladesh by home interior design is here to help you find the best home interior design and decoration services in Bangladesh from professional designers and contractors. You can find interior design for a low prize in Bangladesh. We are a team of Engineers with more than 10 years' experience in the field of uniform development and internal management. We effectively support the activities of Business Houses, Offices, Accommodation, Commercial Construction, Hotels and Restaurants etc. in the National Capital and throughout Bangladesh .If you want to read more about interior design service click here

#Homeinteriordesigninbangladesh
#Interiordesigninbangladesh