Anuruddha
Anuruddha

Reputation: 3245

Converting JSON object array to YAML

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.

Upvotes: 0

Views: 3502

Answers (3)

jpseng
jpseng

Reputation: 2210

yq can be used to convert between JSON and YAML

Convert JSON to YAML

yq -o yaml --prettyPrint
siteidparam: lid
sites:
  - name: default
    routingmethod:
      method: urlparam
      siteid: default
      urlpath: default
  - name: csqcentral
    routingmethod:
      method: urlparam
      siteid: capitolsquare
      urlpath: csq

Convert YAML back to JSON

yq -o json --prettyPrint
{
  "siteidparam": "lid",
  "sites": [
    {
      "name": "default",
      "routingmethod": {
        "method": "urlparam",
        "siteid": "default",
        "urlpath": "default"
      }
    },
    {
      "name": "csqcentral",
      "routingmethod": {
        "method": "urlparam",
        "siteid": "capitolsquare",
        "urlpath": "csq"
      }
    }
  ]
}

(XML and property files can also be transformed using yq)

Upvotes: 0

JesusTinoco
JesusTinoco

Reputation: 11828

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`))
}

Upvotes: 1

CrazyCrow
CrazyCrow

Reputation: 4235

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\n", viper.GetString("siteidparam"))

    sites := viper.Get("sites").([]interface{})
    for i, _ := range sites {
        site := sites[i].(map[interface{}]interface{})
        fmt.Printf("%s\n", site["name"])
        routingmethod := site["routingmethod"].(map[interface{}]interface{})
        fmt.Printf("  %s\n", routingmethod["method"])
        fmt.Printf("  %s\n", routingmethod["siteid"])
        fmt.Printf("  %s\n", routingmethod["urlpath"])
    }
}

Upvotes: 1

Related Questions