flatten

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

  • Owner: jeremywohl/flatten
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

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.

Main metrics

Overview
Name With Ownerjeremywohl/flatten
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2016-01-26 08:19:03
Pushed At2021-12-07 18:51:17
Last Commit At2021-10-12 23:15:45
Release Count2
Last Release Namev1.0.1 (Posted on )
First Release Namev1.0 (Posted on )
用户参与
Stargazers Count225
Watchers Count5
Fork Count49
Commits Count18
Has Issues Enabled
Issues Count7
Issue Open Count3
Pull Requests Count2
Pull Requests Open Count4
Pull Requests Close Count5
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private