Skip to content

Instantly share code, notes, and snippets.

@kcotten
Last active October 10, 2021 20:16
Show Gist options
  • Save kcotten/9bc0305fd53285d30e7bda3ba1046dbe to your computer and use it in GitHub Desktop.
Save kcotten/9bc0305fd53285d30e7bda3ba1046dbe to your computer and use it in GitHub Desktop.
Dynamic json in Go
package main
import (
"encoding/json"
"fmt"
"strings"
)
func main() {
m := deserialize(jsonBlob)
pprint(m)
}
func pprint(m map[string]interface{}) {
fmt.Println("Object")
recurse(m, 1)
}
func recurse(m map[string]interface{}, depth int) {
hyphens := getHyphens(depth)
for k, v := range m {
vType := fmt.Sprintf("%T", v)
switch v.(type) {
case []interface{}:
vType = "Array"
fmt.Printf("%v %v\n", hyphens, vType)
handleInterface(v, depth+1, k)
default:
fmt.Printf("%v %v %v\n", hyphens, k, vType)
}
}
}
func handleInterface(i interface{}, depth int, key string) {
hyphens := getHyphens(depth)
vType := fmt.Sprintf("%T", i)
if rec, ok := i.(map[string]interface{}); ok {
recurse(rec, depth+1)
} else {
switch i.(type) {
case []interface{}:
iSlice, _ := i.([]interface{})
switch iSlice[0].(type) {
case map[string]interface{}:
fmt.Printf("%v %v %v\n", hyphens, key, "Object")
handleInterface(iSlice[0], depth, "")
default:
// Don't increment depth, because the next iteration will be handled by base case of map
handleInterface(iSlice[0], depth, key)
}
default:
fmt.Printf("%v %v %v\n", hyphens, key, vType)
}
}
}
func getHyphens(d int) string {
var sb strings.Builder
for i := 0; i < d*4; i++ {
sb.WriteString("-")
}
return sb.String()
}
func deserialize(js string) map[string]interface{} {
m := map[string]interface{}{}
err := json.Unmarshal([]byte(js), &m)
if err != nil {
panic(err)
}
return m
}
const (
jsonBlob = string(`{
"_id": "61410596ba0c39a1fae2f47f",
"index": 0,
"guid": "fe1102ba-b5f9-4783-a62c-13adb3c2c293",
"isActive": false,
"balance": "$1,500.31",
"picture": "http://placehold.it/32x32",
"age": 33,
"eyeColor": "blue",
"name": "Aguirre Aguilar",
"gender": "male",
"company": "COMTRAK",
"email": "aguirreaguilar@comtrak.com",
"phone": "+1 (828) 595-3377",
"address": "425 Cheever Place, Norfolk, New York, 6272",
"about": "Veniam do incididunt dolor commodo labore ea sint dolore . Nulla amet non Lorem aliquip proident laboris ut. Minim sint ullamco eu magna voluptate. Ut esse cupidatat minim est qui irure ex ullamco qui aliquip.\r\n",
"registered": "2019-05-12T10:09:42 +07:00",
"latitude": 34.248956,
"longitude": 124.1861,
"tags": [
"adipisicing",
"cillum",
"mollit",
"consectetur",
"adipisicing",
"duis",
"adipisicing"
],
"friends": [{
"id": 0,
"name": "Susanne Anderson"
},
{
"id": 1,
"name": "Kara Moon"
},
{
"id": 2,
"name": "Petty Holden"
}
],
"greeting": "Hello, Aguirre Aguilar! You have 4 unread messages.",
"favoriteFruit": "strawberry"
}`)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment