Skip to content
Flatten nested maps or JSON structures to one-dimensional scalar sets.
Go
Branch: master
Clone or download
ruudk and jeremywohl Add underscored style: a_b_c_1_d. (#3)
Add underscore style: a_b_c_1_d.

Thanks Ruud Kamphuis.

Co-authored-by: Ruud Kamphuis <ruudk@users.noreply.github.com>
Latest commit 588fe0d Sep 23, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore start Jan 26, 2016
.travis.yml Add Travis config. Jan 26, 2016
LICENSE start Jan 26, 2016
README.md Add underscored style: a_b_c_1_d. (#3) Sep 23, 2018
TODO start Jan 26, 2016
flatten.go Add underscored style: a_b_c_1_d. (#3) Sep 23, 2018
flatten_test.go Add underscored style: a_b_c_1_d. (#3) Sep 23, 2018

README.md

flatten

GoDoc Build Status

Flatten makes flat, one-dimensional maps from arbitrarily nested ones.

It turns map keys into compound names, in four styles: dotted (a.b.1.c), path-like (a/b/1/c), Rails (a[b][1][c]), or with underscores (a_b_1_c). 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,
// }

See godoc for API.

You can’t perform that action at this time.