Flatten
Flatten nested maps or JSON structures to one-dimensional scalar sets.
Install / Use
/learn @jeremywohl/FlattenREADME
flatten
Note: development continues in v2
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.
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.
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.
emdash := flatten.SeparatorStyle{Middle: "--"}
flat, err := flatten.FlattenString(nested, "", emdash)
// output: `{ "one--two--0": "2a", "one--two--1": "2b", "side": "value" }`
See godoc for API.
Related Skills
node-connect
351.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
