flatten

Flatten nested maps or JSON structures to one-dimensional scalar sets.

  • 所有者: jeremywohl/flatten
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

flatten

GoDoc
Build Status

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.

主要指标

概览
名称与所有者jeremywohl/flatten
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2016-01-26 08:19:03
推送于2021-12-07 18:51:17
最后一次提交2021-10-12 23:15:45
发布数2
最新版本名称v1.0.1 (发布于 )
第一版名称v1.0 (发布于 )
用户参与
星数225
关注者数5
派生数49
提交数18
已启用问题?
问题数7
打开的问题数3
拉请求数2
打开的拉请求数4
关闭的拉请求数5
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?