-
Notifications
You must be signed in to change notification settings - Fork 13
/
gojsonexplode.go
152 lines (145 loc) · 3.19 KB
/
gojsonexplode.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gojsonexplode
// TODO: cannot have delimiter be " or \
import (
"encoding/json"
"errors"
"strconv"
)
func explodeList(l []interface{}, parent string, delimiter string) (map[string]interface{}, error) {
var err error
var key string
j := make(map[string]interface{})
if len(l) == 0 {
key = parent
j[key] = nil
}
for k, i := range l {
if len(parent) > 0 {
key = parent + delimiter + strconv.Itoa(k)
} else {
key = strconv.Itoa(k)
}
switch v := i.(type) {
case nil:
j[key] = v
case int:
j[key] = v
case float64:
j[key] = v
case string:
j[key] = v
case bool:
j[key] = v
case []interface{}:
out := make(map[string]interface{})
out, err = explodeList(v, key, delimiter)
if err != nil {
return nil, err
}
for newkey, value := range out {
j[newkey] = value
}
case map[string]interface{}:
out := make(map[string]interface{})
out, err = explodeMap(v, key, delimiter)
if err != nil {
return nil, err
}
for newkey, value := range out {
j[newkey] = value
}
default:
// do nothing
}
}
return j, nil
}
func explodeMap(m map[string]interface{}, parent string, delimiter string) (map[string]interface{}, error) {
var err error
j := make(map[string]interface{})
for k, i := range m {
if len(parent) > 0 {
k = parent + delimiter + k
}
switch v := i.(type) {
case nil:
j[k] = v
case int:
j[k] = v
case float64:
j[k] = v
case string:
j[k] = v
case bool:
j[k] = v
case []interface{}:
out := make(map[string]interface{})
out, err = explodeList(v, k, delimiter)
if err != nil {
return nil, err
}
for key, value := range out {
j[key] = value
}
case map[string]interface{}:
out := make(map[string]interface{})
out, err = explodeMap(v, k, delimiter)
if err != nil {
return nil, err
}
for key, value := range out {
j[key] = value
}
default:
//nothing
}
}
return j, nil
}
// Explodejson takes in a nested JSON as a byte array and a delimiter and returns an
// exploded/flattened json byte array
func Explodejson(b []byte, d string) ([]byte, error) {
var input interface{}
var exploded map[string]interface{}
var out []byte
var err error
err = json.Unmarshal(b, &input)
if err != nil {
return nil, err
}
switch t := input.(type) {
case map[string]interface{}:
exploded, err = explodeMap(t, "", d)
if err != nil {
return nil, err
}
case []interface{}:
exploded, err = explodeList(t, "", d)
if err != nil {
return nil, err
}
default:
// How did we get here? It is impossible!!
return nil, errors.New("Possible error in JSON")
}
out, err = json.Marshal(exploded)
if err != nil {
return nil, err
}
return out, nil
}
// Explodejsonstr explodes a nested JSON string to an unnested one
// parameters to pass to the function are
// * s: the JSON string
// * d: the delimiter to use when unnesting the JSON object.
// {"person":{"name":"Joe", "address":{"street":"123 Main St."}}}
// explodes to:
// {"person.name":"Joe", "person.address.street":"123 Main St."}
func Explodejsonstr(s string, d string) (string, error) {
b := []byte(s)
out, err := Explodejson(b, d)
if err != nil {
return "", err
}
return string(out), nil
}