kenfire
kenfire

Reputation: 1355

How to read multiple JSON objects from one file in Go

How can I read a JSON file where there is two different objects using Unmarshal ?

JSON example:

This is the structure corresponding to the JSON file.

{
  "mysql": {
    "address": "127.0.0.1",
    "port": "3306",
    "user": "user",
    "password": "password",
    "database": "database"
  },
  "postgres": {
    "address": "127.0.0.2",
    "port": "3306",
    "user": "user2",
    "password": "password2",
    "database": "database2"
  }
}

Code snippet:

type Database struct {
    Address  string
    Port     string
    User     string
    Password string
    Database string
}
type Mysql struct {
    Database
}
type Postgres struct {
    Database
}

Upvotes: 1

Views: 3217

Answers (2)

wasmup
wasmup

Reputation: 16233

Just another way:

m := make(map[string]Db)
err := json.Unmarshal([]byte(input), &m)

try this:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    m := make(map[string]Db)
    err := json.Unmarshal([]byte(input), &m)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(m)
}

type Db struct {
    Address  string
    Port     string
    User     string
    Password string
    Database string
}

const input = `{
  "mysql": {
    "address": "127.0.0.1",
    "port": "3306",
    "user": "user",
    "password": "password",
    "database": "database"
  },
  "postgres": {
    "address": "127.0.0.2",
    "port": "3306",
    "user": "user2",
    "password": "password2",
    "database": "database2"
  }
}`

output:

map[mysql:{127.0.0.1 3306 user password database} postgres:{127.0.0.2 3306 user2 password2 database2}]

Even this works for you:

m := make(map[string]map[string]string)
err := json.Unmarshal([]byte(input), &m)

try this:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    m := make(map[string]map[string]string)
    err := json.Unmarshal([]byte(input), &m)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(m)
}

const input = `{
  "mysql": {
    "address": "127.0.0.1",
    "port": "3306",
    "user": "user",
    "password": "password",
    "database": "database"
  },
  "postgres": {
    "address": "127.0.0.2",
    "port": "3306",
    "user": "user2",
    "password": "password2",
    "database": "database2"
  }
}`

output:

map[mysql:map[address:127.0.0.1 port:3306 user:user password:password database:database] postgres:map[database:database2 address:127.0.0.2 port:3306 user:user2 password:password2]]

Upvotes: 1

kenfire
kenfire

Reputation: 1355

To do this, you need to wrap the Mysql and Postgres structure into a Configuration structure then pass it to Unmarshal function:

type Configuration struct {
    Mysql    Mysql
    Postgres Postgres
}

func main() {
    content, err := ioutil.ReadFile(confPath)
    var conf Configuration
    err = json.Unmarshal(content, &conf)
}

See full working example: https://play.golang.org./p/7CtALgsjK3

Hope this will help some people.

Upvotes: 5

Related Questions