Documentation
¶
Overview ¶
Flatten makes flat, one-dimensional maps from arbitrarily nested ones.
Map keys turn into compound names, like `a.b.1.c` (dotted style) or `a[b][1][c]` (Rails style). It takes input as either JSON strings or Go structures. It (only) knows how to traverse JSON types: maps, slices and scalars.
You can flatten JSON strings.
nested := `{
"one": {
"two": [
"2a",
"2b"
]
},
"side": "value"
}`
flat, err := FlattenString(nested, "", DotStyle)
// output: `{ "one.two.0": "2a", "one.two.1": "2b", "side": "value" }`
Or Go maps directly.
t := map[string]interface{}{
"a": "b",
"c": map[string]interface{}{
"d": "e",
"f": "g",
},
"z": 1.4567,
}
flat, err := Flatten(nested, "", RailsStyle)
// output:
// map[string]interface{}{
// "a": "b",
// "c[d]": "e",
// "c[f]": "g",
// "z": 1.4567,
// }
Click to show internal directories.
Click to hide internal directories.