The package flat
provides methods to handle JSON, XML or YAML data as a map[string]interface{}
,
useful to manipulate unknown structures or to flatten them into a single dimension.
To install it, you need to install Go and set your Go workspace first. Then, download and install it:
$ go get -u github.com/rvflash/flat
Import it in your code:
import "github.com/rvflash/flat"
flat
uses the Go modules that required Go 1.11 or later.
Errors are ignored just for the demo.
var (
d = map[string]interface{}{
"languages": map[string]interface{}{
"en": "English",
"fr": "French",
},
}
res = bytes.Buffer{}
attrs = []xml.Attr{
{Name: xml.Name{Local: "xmlns:xsi"}, Value: "http://www.w3.org/2001/XMLSchema-instance"},
}
_ = flat.New(
d,
flat.XMLName("custom"),
flat.XMLNS("http://schemas.xmlsoap.org/soap/envelope/"),
flat.XMLAttributes(attrs),
).XMLEncode(&res)
)
fmt.Println(res.String())
// Output:
// <custom xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><languages><en>English</en><fr>French</fr></languages></custom>
var (
d = flat.D{}
_ = xml.Unmarshal([]byte(`<custom><languages><en>English</en><fr>French</fr></languages></custom>`), &d)
)
fmt.Printf("%#v", d.Flatten())
// Output:
// map[string]interface {}{"languages_en":"English", "languages_fr":"French"}