In go, we can read a JSON file and put its contents into a struct using the encoding/json
package.
First, we define the struct with corresponding field names and types that matches the JSON file:
main.go110 chars6 lines
Then, we open and read the JSON file into a byte slice:
main.go167 chars11 lines
Finally, we unmarshal the byte slice into the struct using json.Unmarshal()
:
main.go185 chars10 lines
Here's the complete code:
main.go639 chars39 lines
gistlibby LogSnag