Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten nested JSON

Trying to flatten a nested json response from 2 levels deep down to 1.

Here is my working code on Go Playground: http://play.golang.org/p/kHAYuZUTko

I want to end up with:

type Social struct {
    GooglePlusPlusOnes  uint32 `Social:"GooglePlusOne"`
    TwitterTweets       uint32 `json:"Twitter"`
    LinkedinShares      uint32 `json:"LinkedIn"`
    PinterestPins       uint32 `json:"Pinterest"`
    StumbleuponStumbles uint32 `json:"StumbleUpon"`
    DeliciousBookmarks  uint32 `json:"Delicious"`
    FacebookLikes       uint32 `json:"??some_magical_nested_address??"`
    FacebookShares      uint32 `json:"??some_magical_nested_address??"`
    FacebookComments    uint32 `json:"??some_magical_nested_address??"`
    FacebookTotal       uint32 `json:"??some_magical_nested_address??"`
}

...instead of a Social type containing a nested Facebook type (seen in the code below).

I suspect I'll need to do a custom UnmarshalJSON function, but I don't know how those work and I'm new to Go.

Suggestions?


Here is the code from the Go Playground link above:

package main

import (
    "encoding/json"
    "fmt"
)

type Social struct {
    GooglePlusPlusOnes  uint32 `json:"GooglePlusOne"`
    TwitterTweets       uint32 `json:"Twitter"`
    LinkedinShares      uint32 `json:"LinkedIn"`
    PinterestPins       uint32 `json:"Pinterest"`
    StumbleuponStumbles uint32 `json:"StumbleUpon"`
    DeliciousBookmarks  uint32 `json:"Delicious"`
    Facebook            Facebook
}

type Facebook struct {
    FacebookLikes    uint32 `json:"like_count"`
    FacebookShares   uint32 `json:"share_count"`
    FacebookComments uint32 `json:"comment_count"`
    FacebookTotal    uint32 `json:"total_count"`
}

func main() {
    var jsonBlob = []byte(`[
        {"StumbleUpon":0,"Reddit":0,"Facebook":{"commentsbox_count":4691,"click_count":0,"total_count":298686,"comment_count":38955,"like_count":82902,"share_count":176829},"Delicious":0,"GooglePlusOne":275234,"Buzz":0,"Twitter":7346788,"Diggs":0,"Pinterest":40982,"LinkedIn":0}
    ]`)

    var social []Social
    err := json.Unmarshal(jsonBlob, &social)
    if err != nil {
        fmt.Println("error:", err)
    }
    fmt.Printf("%+v", social)
}
like image 529
Joe Bergevin Avatar asked Jul 08 '14 22:07

Joe Bergevin


People also ask

How do I flatten a nested JSON file?

Pandas have a nice inbuilt function called json_normalize() to flatten the simple to moderately semi-structured nested JSON structures to flat tables. Parameters: data – dict or list of dicts.


1 Answers

If you are only using maps this function will be useful.

// Flatten takes a map and returns a new one where nested maps are replaced
// by dot-delimited keys.
func Flatten(m map[string]interface{}) map[string]interface{} {
    o := make(map[string]interface{})
    for k, v := range m {
            switch child := v.(type) {
            case map[string]interface{}:
                    nm := Flatten(child)
                    for nk, nv := range nm {
                            o[k+"."+nk] = nv
                    }
            default:
                    o[k] = v
            }
    }
    return o
}
like image 60
jake_astub Avatar answered Sep 16 '22 14:09

jake_astub