I'm trying to create JSON to send to a Rails API that accepts the following format:
{"device":{"ipaddress":"192.168.1.2", "netmask": "255.255.255.0", "gateway": "192.168.1.1"}}
But I'm unsure of how to wrap what I've already marshaled into the "device":{}
portion that the API accepts.
Here's what I have so far:
type Device struct {
IPAddress string `json:"ipaddress"`
Network string `json:"network"`
Gateway string `json:"gateway"`
}
// gathers the IP info
func GatherIPInfo() {
d := Device{
IPAddress: "192.168.1.2",
Network: "255.255.255.0",
Gateway: "192.168.1.1",
}
// now send to API
data, _ := json.Marshal(d) // looks like: {"ipaddress":"192.168.1.2","network":"255.255.255.0","gateway":"192.168.1.1"}
...
...
}
Do I have to create another struct to be able to achieve wrapping the existing into 'device' or is there an easier way?
Thanks!
You can create another struct to enclose the Device struct you have, or do something like this:
json.Marshal(map[string]interface{}{"device":d})