-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (55 loc) · 1.24 KB
/
main.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
//https://stackoverflow.com/questions/40343471/how-to-check-if-interface-is-a-slice
//https://www.json.org/json-en.html
//https://datatracker.ietf.org/doc/html/rfc7159
//https://earthly.dev/blog/jq-select/
//value: object, array, number, or string, "false", "null", "true"
import (
"encoding/json"
"fmt"
"io"
"os"
)
func main() {
jsonFile, err := os.Open("file2.json")
if err != nil {
panic(err)
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
panic(err)
}
//var data map[string]any
var data any
err = json.Unmarshal([]byte(byteValue), &data)
if err != nil {
fmt.Println(err)
}
switch d := data.(type) {
case string:
fmt.Println("string ", d)
case []interface{}:
fmt.Println("is an array:", d)
for i, u := range d {
fmt.Println(i, u)
}
case map[string]interface{}:
fmt.Println("is a map")
default:
fmt.Println("is of a type I don't know how to handle")
}
//if reflect.TypeOf(data).Kind() == reflect.Slice {
// for i := range data {
// fmt.Printf("%s\n", data[i])
//
//
// reflect.TypeOf(data).Kind() == reflect.Map {
// for k, v := range data {
// fmt.Printf("%s: %s\n", k, v)
// }
//
//for k, v := range data {
// fmt.Printf("KEY: %s Value: %s\n", k, v)
//}
}