I have the following json which needs to convert to YAML
{
"siteidparam": "lid",
"sites": [
{
"name": "default",
"routingmethod": {
"method": "urlparam",
"siteid": "default",
"urlpath": "default"
}
},
{
"name": "csqcentral",
"routingmethod": {
"method": "urlparam",
"siteid": "capitolsquare",
"urlpath": "csq"
}
}
]
}
I used online JSON to YAML converter and it gave the following output,
---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"
when I tried to convert the same generated YAML back to json from the online service, it gives "Unable to parse" exception.
1.) what is the correct way of representing above kind of jsons in YAML?
I want to read this kind of YAML inside my golang program. For that I'm using spf13/viper library, but I couldn't find any method which is able to decode this king of array objects.
2.) How to read this kind of YAML in golang using viper? Sample code would help.
Code is ugly but looks like this library does not like nested arrays of objects.
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigType("yaml")
var yamlExample = []byte(`---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"`)
viper.ReadConfig(bytes.NewReader(yamlExample))
fmt.Printf("%s
", viper.GetString("siteidparam"))
sites := viper.Get("sites").([]interface{})
for i, _ := range sites {
site := sites[i].(map[interface{}]interface{})
fmt.Printf("%s
", site["name"])
routingmethod := site["routingmethod"].(map[interface{}]interface{})
fmt.Printf(" %s
", routingmethod["method"])
fmt.Printf(" %s
", routingmethod["siteid"])
fmt.Printf(" %s
", routingmethod["urlpath"])
}
}
The issue with parsing your YAML to JSON is that it has two spaces in each items. It should be like this:
---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"
About your second question find below a simple snippet about how to achive that:
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
var yamlExample2 = []byte(`
---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"
`)
viper.ReadConfig(bytes.NewBuffer(yamlExample2))
fmt.Println(viper.Get(`sites`))
}