-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonTest.go
More file actions
43 lines (39 loc) · 1.01 KB
/
Copy pathjsonTest.go
File metadata and controls
43 lines (39 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"fmt"
"reflect"
yaml "sigs.k8s.io/yaml"
)
func main(){
testJson := []byte(`{"interfaces": ["name": "test1", "name": "123", "name": "3.14", "name": "0e7480436552138"]}`)
testYaml := []byte(`interfaces:
- name: 01c6e4e33a6b25a
- name: 0e7480436552138
`)
var state map[string]interface{}
var stateJson map[string]interface{}
err := yaml.Unmarshal(testJson, &stateJson)
if err != nil {
fmt.Println("ERROR: ", err)
}
err = yaml.Unmarshal(testYaml, &state)
if err != nil {
fmt.Println("ERROR: ", err)
}
// JSON
fmt.Println("JSON")
interfaces := stateJson["interfaces"]
for _, iface := range interfaces.([]interface{}) {
name := iface.(map[string]interface{})["name"]
fmt.Println(reflect.TypeOf(name.(string)))
fmt.Println(name.(string))
}
// YAML
fmt.Println("\nYAML")
interfaces = state["interfaces"]
for _, iface := range interfaces.([]interface{}) {
name := iface.(map[string]interface{})["name"]
fmt.Println(reflect.TypeOf(name.(string)))
fmt.Println(name.(string))
}
}