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
|
flatten
=======
[](https://godoc.org/github.com/jeremywohl/flatten)
[](https://travis-ci.org/jeremywohl/flatten)
Flatten makes flat, one-dimensional maps from arbitrarily nested ones.
It turns map keys into compound
names, in four default styles: dotted (`a.b.1.c`), path-like (`a/b/1/c`), Rails (`a[b][1][c]`), or with underscores (`a_b_1_c`). Alternatively, you can pass a custom style.
It takes input as either JSON strings or
Go structures. It knows how to traverse these JSON types: objects/maps, arrays and scalars.
You can flatten JSON strings.
```go
nested := `{
"one": {
"two": [
"2a",
"2b"
]
},
"side": "value"
}`
flat, err := flatten.FlattenString(nested, "", flatten.DotStyle)
// output: `{ "one.two.0": "2a", "one.two.1": "2b", "side": "value" }`
```
Or Go maps directly.
```go
nested := map[string]interface{}{
"a": "b",
"c": map[string]interface{}{
"d": "e",
"f": "g",
},
"z": 1.4567,
}
flat, err := flatten.Flatten(nested, "", flatten.RailsStyle)
// output:
// map[string]interface{}{
// "a": "b",
// "c[d]": "e",
// "c[f]": "g",
// "z": 1.4567,
// }
```
Let's try a custom style, with the first example above.
```go
emdash := flatten.SeparatorStyle{Middle: "--"}
flat, err := flatten.FlattenString(nested, "", emdash)
// output: `{ "one--two--0": "2a", "one--two--1": "2b", "side": "value" }`
```
See [godoc](https://godoc.org/github.com/jeremywohl/flatten) for API.
|