1

Golang newbie here

I'm trying to parse from a .json file (in the same directory as the Go code) into a struct holding other structs and the closest I can get to success is a struct containing boolean false, which sounds broken to me.

Here's what I have in my Go code so far

package main

import (
    "encoding/json"
    "fmt"

    "io/ioutil"
)

type App struct {
    Name string `json:"app:name"`
}

type Database struct {
    Type     string `json:"database:type"`
    Name     string `json:"database:name"`
    User     string `json:"database:user"`
    Password string `json:"database:password"`
}

type Environment struct {
    Mode  string `json:"environment:mode"`
    Debug bool   `json:"environment:debug"`
}

type Config struct {
    Environment Environment
    App         App
    Database    Database
}

func main() {
    config, err := ioutil.ReadFile("config.json")
    if err != nil {
        fmt.Errorf("Error reading config file: %s", err)
    }

    var appSettings Config
    json.Unmarshal(config, &appSettings)

    fmt.Print(appSettings)
}

and here's the contents of my .json file

{
  "App": {
    "Name": "My_Project"
  },
  "Database": {
    "Type": "postgres",
    "Name": "my_project_db_name",
    "User": "my_project_db_user",
    "Password": "secret"
  },
  "Environment": {
    "Mode": "development",
    "Debug": true
  }
}

EDIT: Here's the result of the print at the end of main()

{{ false} {} { }}

I have already validated the json content which is fine. The struct names and properties are being exported. Can you see what I'm doing wrong?

1
  • Your tags are completely wrong. If the JSON field name matches the fieldname of your struct there is no need for a tag. And what should "environment:mode" do? why the "environment:"? Just drop all your tags, read the documentation and the examples once more. Commented Jan 8, 2017 at 18:51

1 Answer 1

8

Can you try by changing like this:

type App struct {
    Name string `json:"name"`
}

type Database struct {
    Type     string `json:"type"`
    Name     string `json:"name"`
    User     string `json:"user"`
    Password string `json:"password"`
}

type Environment struct {
    Mode  string `json:"mode"`
    Debug bool   `json:"debug"`
}

Here is the output:

{{development true} {My_Project} {postgres my_project_db_name my_project_db_user secret}}

Here is a little docs for handy reference:

// Field is ignored by this package.
Field int `json:"-"`

// Field appears in JSON as key "myName".
Field int `json:"myName"`

// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`

// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.