Skip to content

Latest commit

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

flatmap

flatmap transform nested map to flatten map

How to use?

package main
import (
"fmt"
"log"
"sort"
"encoding/json"
"github.com/astaxie/flatmap"
)
var data =`
{
"id": 1296269,
"owner": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"description": "This your first repo!",
"private": false,
"fork": false,
"url": "https://api.github.com/repos/octocat/Hello-World",
"html_url": "https://github.com/octocat/Hello-World",
"clone_url": "https://github.com/octocat/Hello-World.git",
"git_url": "git://github.com/octocat/Hello-World.git",
"ssh_url": "git@github.com:octocat/Hello-World.git",
"svn_url": "https://svn.github.com/octocat/Hello-World",
"mirror_url": "git://git.example.com/octocat/Hello-World",
"homepage": "https://github.com",
"language": null,
"forks_count": 9,
"stargazers_count": 80,
"watchers_count": 80,
"size": 108,
"default_branch": "master",
"open_issues_count": 0,
"has_issues": true,
"has_wiki": true,
"has_downloads": true,
"pushed_at": "2011-01-26T19:06:43Z",
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
"permissions": {
"admin": false,
"push": false,
"pull": true
},
"subscribers_count": 42,
"organization": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "Organization",
"site_admin": false
},
"parent": {
"id": 1296269,
"owner": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"description": "This your first repo!",
"private": false,
"fork": true,
"url": "https://api.github.com/repos/octocat/Hello-World",
"html_url": "https://github.com/octocat/Hello-World",
"clone_url": "https://github.com/octocat/Hello-World.git",
"git_url": "git://github.com/octocat/Hello-World.git",
"ssh_url": "git@github.com:octocat/Hello-World.git",
"svn_url": "https://svn.github.com/octocat/Hello-World",
"mirror_url": "git://git.example.com/octocat/Hello-World",
"homepage": "https://github.com",
"language": null,
"forks_count": 9,
"stargazers_count": 80,
"watchers_count": 80,
"size": 108,
"default_branch": "master",
"open_issues_count": 0,
"has_issues": true,
"has_wiki": true,
"has_downloads": true,
"pushed_at": "2011-01-26T19:06:43Z",
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
"permissions": {
"admin": false,
"push": false,
"pull": true
}
},
"source": {
"id": 1296269,
"owner": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"description": "This your first repo!",
"private": false,
"fork": true,
"url": "https://api.github.com/repos/octocat/Hello-World",
"html_url": "https://github.com/octocat/Hello-World",
"clone_url": "https://github.com/octocat/Hello-World.git",
"git_url": "git://github.com/octocat/Hello-World.git",
"ssh_url": "git@github.com:octocat/Hello-World.git",
"svn_url": "https://svn.github.com/octocat/Hello-World",
"mirror_url": "git://git.example.com/octocat/Hello-World",
"homepage": "https://github.com",
"language": null,
"forks_count": 9,
"stargazers_count": 80,
"watchers_count": 80,
"size": 108,
"default_branch": "master",
"open_issues_count": 0,
"has_issues": true,
"has_wiki": true,
"has_downloads": true,
"pushed_at": "2011-01-26T19:06:43Z",
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
"permissions": {
"admin": false,
"push": false,
"pull": true
}
}
}
`
func main() {
var mp map[string]interface{}
if err := json.Unmarshal([]byte(data), &mp); err != nil {
log.Fatal(err)
}
fm, err := flatmap.Flatten(mp)
if err != nil {
log.Fatal(err)
}
var ks []string
for k:=range fm{
ks = append(ks,k) }
sort.Strings(ks)
for _,k:=range ks{
fmt.Println(k,":",fm[k])
}
}

output

KeyValue
clone_urlhttps://github.com/octocat/Hello-World.git
created_at2011-01-26T19:01:12Z
default_branchmaster
descriptionThis your first repo!
forkfalse
forks_count9.000000
full_nameoctocat/Hello-World
git_urlgit://github.com/octocat/Hello-World.git
has_downloadstrue
has_issuestrue
has_wikitrue
homepagehttps://github.com
html_urlhttps://github.com/octocat/Hello-World
id1296269.000000
language
mirror_urlgit://git.example.com/octocat/Hello-World
nameHello-World
open_issues_count0.000000
organization.avatar_urlhttps://github.com/images/error/octocat_happy.gif
organization.events_urlhttps://api.github.com/users/octocat/events{/privacy}
organization.followers_urlhttps://api.github.com/users/octocat/followers
organization.following_urlhttps://api.github.com/users/octocat/following{/other_user}
organization.gists_urlhttps://api.github.com/users/octocat/gists{/gist_id}
organization.gravatar_idsomehexcode
organization.html_urlhttps://github.com/octocat
organization.id1.000000
organization.loginoctocat
organization.organizations_urlhttps://api.github.com/users/octocat/orgs
organization.received_events_urlhttps://api.github.com/users/octocat/received_events
organization.repos_urlhttps://api.github.com/users/octocat/repos
organization.site_adminfalse
organization.starred_urlhttps://api.github.com/users/octocat/starred{/owner}{/repo}
organization.subscriptions_urlhttps://api.github.com/users/octocat/subscriptions
organization.typeOrganization
organization.urlhttps://api.github.com/users/octocat
owner.avatar_urlhttps://github.com/images/error/octocat_happy.gif
owner.events_urlhttps://api.github.com/users/octocat/events{/privacy}
owner.followers_urlhttps://api.github.com/users/octocat/followers
owner.following_urlhttps://api.github.com/users/octocat/following{/other_user}
owner.gists_urlhttps://api.github.com/users/octocat/gists{/gist_id}
owner.gravatar_idsomehexcode
owner.html_urlhttps://github.com/octocat
owner.id1.000000
owner.loginoctocat
owner.organizations_urlhttps://api.github.com/users/octocat/orgs
owner.received_events_urlhttps://api.github.com/users/octocat/received_events
owner.repos_urlhttps://api.github.com/users/octocat/repos
owner.site_adminfalse
owner.starred_urlhttps://api.github.com/users/octocat/starred{/owner}{/repo}
owner.subscriptions_urlhttps://api.github.com/users/octocat/subscriptions
owner.typeUser
owner.urlhttps://api.github.com/users/octocat
parent.clone_urlhttps://github.com/octocat/Hello-World.git
parent.created_at2011-01-26T19:01:12Z
parent.default_branchmaster
parent.descriptionThis your first repo!
parent.forktrue
parent.forks_count9.000000
parent.full_nameoctocat/Hello-World
parent.git_urlgit://github.com/octocat/Hello-World.git
parent.has_downloadstrue
parent.has_issuestrue
parent.has_wikitrue
parent.homepagehttps://github.com
parent.html_urlhttps://github.com/octocat/Hello-World
parent.id1296269.000000
parent.language
parent.mirror_urlgit://git.example.com/octocat/Hello-World
parent.nameHello-World
parent.open_issues_count0.000000
parent.owner.avatar_urlhttps://github.com/images/error/octocat_happy.gif
parent.owner.events_urlhttps://api.github.com/users/octocat/events{/privacy}
parent.owner.followers_urlhttps://api.github.com/users/octocat/followers
parent.owner.following_urlhttps://api.github.com/users/octocat/following{/other_user}
parent.owner.gists_urlhttps://api.github.com/users/octocat/gists{/gist_id}
parent.owner.gravatar_idsomehexcode
parent.owner.html_urlhttps://github.com/octocat
parent.owner.id1.000000
parent.owner.loginoctocat
parent.owner.organizations_urlhttps://api.github.com/users/octocat/orgs
parent.owner.received_events_urlhttps://api.github.com/users/octocat/received_events
parent.owner.repos_urlhttps://api.github.com/users/octocat/repos
parent.owner.site_adminfalse
parent.owner.starred_urlhttps://api.github.com/users/octocat/starred{/owner}{/repo}
parent.owner.subscriptions_urlhttps://api.github.com/users/octocat/subscriptions
parent.owner.typeUser
parent.owner.urlhttps://api.github.com/users/octocat
parent.permissions.adminfalse
parent.permissions.pulltrue
parent.permissions.pushfalse
parent.privatefalse
parent.pushed_at2011-01-26T19:06:43Z
parent.size108.000000
parent.ssh_urlgit@github.com:octocat/Hello-World.git
parent.stargazers_count80.000000
parent.svn_urlhttps://svn.github.com/octocat/Hello-World
parent.updated_at2011-01-26T19:14:43Z
parent.urlhttps://api.github.com/repos/octocat/Hello-World
parent.watchers_count80.000000
permissions.adminfalse
permissions.pulltrue
permissions.pushfalse
privatefalse
pushed_at2011-01-26T19:06:43Z
size108.000000
source.clone_urlhttps://github.com/octocat/Hello-World.git
source.created_at2011-01-26T19:01:12Z
source.default_branchmaster
source.descriptionThis your first repo!
source.forktrue
source.forks_count9.000000
source.full_nameoctocat/Hello-World
source.git_urlgit://github.com/octocat/Hello-World.git
source.has_downloadstrue
source.has_issuestrue
source.has_wikitrue
source.homepagehttps://github.com
source.html_urlhttps://github.com/octocat/Hello-World
source.id1296269.000000
source.language
source.mirror_urlgit://git.example.com/octocat/Hello-World
source.nameHello-World
source.open_issues_count0.000000
source.owner.avatar_urlhttps://github.com/images/error/octocat_happy.gif
source.owner.events_urlhttps://api.github.com/users/octocat/events{/privacy}
source.owner.followers_urlhttps://api.github.com/users/octocat/followers
source.owner.following_urlhttps://api.github.com/users/octocat/following{/other_user}
source.owner.gists_urlhttps://api.github.com/users/octocat/gists{/gist_id}
source.owner.gravatar_idsomehexcode
source.owner.html_urlhttps://github.com/octocat
source.owner.id1.000000
source.owner.loginoctocat
source.owner.organizations_urlhttps://api.github.com/users/octocat/orgs
source.owner.received_events_urlhttps://api.github.com/users/octocat/received_events
source.owner.repos_urlhttps://api.github.com/users/octocat/repos
source.owner.site_adminfalse
source.owner.starred_urlhttps://api.github.com/users/octocat/starred{/owner}{/repo}
source.owner.subscriptions_urlhttps://api.github.com/users/octocat/subscriptions
source.owner.typeUser
source.owner.urlhttps://api.github.com/users/octocat
source.permissions.adminfalse
source.permissions.pulltrue
source.permissions.pushfalse
source.privatefalse
source.pushed_at2011-01-26T19:06:43Z
source.size108.000000
source.ssh_urlgit@github.com:octocat/Hello-World.git
source.stargazers_count80.000000
source.svn_urlhttps://svn.github.com/octocat/Hello-World
source.updated_at2011-01-26T19:14:43Z
source.urlhttps://api.github.com/repos/octocat/Hello-World
source.watchers_count80.000000
ssh_urlgit@github.com:octocat/Hello-World.git
stargazers_count80.000000
subscribers_count42.000000
svn_urlhttps://svn.github.com/octocat/Hello-World
updated_at2011-01-26T19:14:43Z
urlhttps://api.github.com/repos/octocat/Hello-World
watchers_count80.000000

About

flatmap transform nested map to flatten map

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages