Reputation: 21629
Consider the following code:
package main
import (
"encoding/json"
"fmt"
"reflect"
)
func main() {
//Creating the maps for JSON
m := map[string]interface{}{}
//Parsing/Unmarshalling JSON encoding/json
err := json.Unmarshal([]byte(input), &m)
fmt.Println("\nReflect type of Parsing/Unmarshalling Error Object:\n",reflect.TypeOf(err))
fmt.Println("\nParsing/Unmarshalling Error Object:\n",err)
if err != nil {
panic(err)
}
fmt.Println("\nParsed JSON is as follows:\n",m)
fmt.Println("\nReflect type of parsed json object:\n", reflect.TypeOf(m))
for firstLvlkey, firstLvlValue := range m {
fmt.Println("First Level Key:", firstLvlkey)
fmt.Println("First Level Key reflect type of :", reflect.TypeOf(firstLvlkey))
fmt.Println("First Level Value:", firstLvlValue)
fmt.Println("First Level Value reflect type of :", reflect.TypeOf(firstLvlValue))
// <===============================>
//Here I want to iterate/loop over innerJSON1, InnerJSON2 then reach to level InnerInnerJSONArray - fld1 and fld2
// <===============================>
}
}
const input = `
{
"outterJSON":{
"innerJSON1":{
"value1":10,
"value2":22
,
"InnerInnerArray": [ "test1" , "test2"],
"InnerInnerJSONArray": [ {"fld1" : "val1"} , {"fld2" : "val2"} ]
},
"InnerJSON2":"NoneValue"
}
}
`
I have some requirement like I want to read/get all the Key and value in String
type for some processing adn I can't define the struct
because I will be getting dynamic JSON input (e.g InnerInnerArray
as a string then second level loop will give me index of array and process each JSON having key fld1
and val1
).
I wish to iterate over every key/value pair contained within it, what is the most efficient way of going through the map?
Note: I am Newbie for Go-lang, your suggestion/improvement on question is also most welcome.
Upvotes: 13
Views: 24584
Reputation: 106
I have implemented something very similar to https://stackoverflow.com/users/1078890/iamnan solution. The body of parseMap
and parseArray
is combined into one and it looks something like this.
func printJson(res1 map[string]interface{}, res2 []interface{}) {
for k, v := range res1 {
switch vv := v.(type) {
case float64, int, string:
fmt.Println(k, ":", vv)
case []interface{}:
fmt.Println(k, ":")
printJson(nil, vv)
case map[string]interface{}:
fmt.Println(k, ":")
printJson(vv, nil)
default:
fmt.Println(k, ":", vv)
}
}
for k, v := range res2 {
switch vv := v.(type) {
case float64, int, string:
fmt.Println(k, ":", vv)
case []interface{}:
fmt.Println(k, ":")
printJson(nil, vv)
case map[string]interface{}:
fmt.Println(k, ":")
printJson(vv, nil)
default:
fmt.Println(k, ":", vv)
}
}
}
Code can be found here https://gist.github.com/sandeep-sarkar/78a0e96461b4dec727386a96404d29b0
Upvotes: 0
Reputation: 6864
See this blog entry which thoroughly covers this subject, specifically the section Decoding arbitrary data. Using that you can do something like this: (playground example)
package main
import (
"encoding/json"
"fmt"
)
func main() {
// Creating the maps for JSON
m := map[string]interface{}{}
// Parsing/Unmarshalling JSON encoding/json
err := json.Unmarshal([]byte(input), &m)
if err != nil {
panic(err)
}
parseMap(m)
}
func parseMap(aMap map[string]interface{}) {
for key, val := range aMap {
switch concreteVal := val.(type) {
case map[string]interface{}:
fmt.Println(key)
parseMap(val.(map[string]interface{}))
case []interface{}:
fmt.Println(key)
parseArray(val.([]interface{}))
default:
fmt.Println(key, ":", concreteVal)
}
}
}
func parseArray(anArray []interface{}) {
for i, val := range anArray {
switch concreteVal := val.(type) {
case map[string]interface{}:
fmt.Println("Index:", i)
parseMap(val.(map[string]interface{}))
case []interface{}:
fmt.Println("Index:", i)
parseArray(val.([]interface{}))
default:
fmt.Println("Index", i, ":", concreteVal)
}
}
}
const input = `
{
"outterJSON": {
"innerJSON1": {
"value1": 10,
"value2": 22,
"InnerInnerArray": [ "test1" , "test2"],
"InnerInnerJSONArray": [{"fld1" : "val1"} , {"fld2" : "val2"}]
},
"InnerJSON2":"NoneValue"
}
}
`
This will print:
//outterJSON
//innerJSON1
//InnerInnerJSONArray
//Index: 0
//fld1 : val1
//Index: 1
//fld2 : val2
//value1 : 10
//value2 : 22
//InnerInnerArray
//Index 0 : test1
//Index 1 : test2
//InnerJSON2 : NoneValue
The key thing is that you have to use type assertion when working with interface types. The type switch makes it easy to determine the type as needed. The code will recursively range through any nested array or map so you can add as many levels as you wish and get all your values.
Upvotes: 29
Reputation: 10228
There are related questions here and here (and possibly others).
There are some more sophisticated JSON parsing APIs that make your job easier. An example is stretchr/objx.
An example of using objx:
document, err := objx.FromJSON(json)
// TODO handle err
document.Get("path.to.field[0].you.want").Str()
This works when you really don't know what the JSON structure will be. However, if you do know the structure of your JSON input ahead of time, the preferred way is to describe it with structs and use the standard API for marshalling.
Upvotes: 5
Reputation: 156364
You'll need to parse the JSON and then recurse through the structure inspecting the types of the contained values and handling them in some way.
The example function below takes an *interface{}
(pointer to any type) and a handler function of string, int, and object pointers to which it yields the items it discovers:
func eachJsonValue(obj *interface{}, handler func(*string, *int, *interface{})) {
if obj == nil {
return
}
// Yield all key/value pairs for objects.
o, isObject := (*obj).(map[string]interface{})
if isObject {
for k, v := range o {
handler(&k, nil, &v)
eachJsonValue(&v, handler)
}
}
// Yield each index/value for arrays.
a, isArray := (*obj).([]interface{})
if isArray {
for i, x := range a {
handler(nil, &i, &x)
eachJsonValue(&x, handler)
}
}
// Do nothing for primitives since the handler got them.
}
Calling it as demonstrated below will print the listed results. Your handler function could, of course, do something special with known key/values such as "fld1":
func main() {
// Parse the JSON.
var obj interface{}
json.Unmarshal([]byte(input), &obj) // XXX: check the error value.
// Handle object key/value pairs and array index/items.
eachJsonValue(&obj, func(key *string, index *int, value *interface{}) {
if key != nil { // It's an object key/value pair...
fmt.Printf("OBJ: key=%q, value=%#v\n", *key, *value)
} else { // It's an array item...
fmt.Printf("ARR: index=%d, value=%#v\n", *index, *value)
}
})
}
// OBJ: key="outterJSON", value=map[string]interface {}{...}
// OBJ: key="innerJSON1", value=map[string]interface {}{...}
// OBJ: key="value1", value=10
// OBJ: key="value2", value=22
// OBJ: key="InnerInnerArray", value=[]interface {}{...}
// ARR: index=0, value="test1"
// ARR: index=1, value="test2"
// OBJ: key="InnerInnerJSONArray", value=[]interface {}{...}
// ARR: index=0, value=map[string]interface {}{...}
// OBJ: key="fld1", value="val1"
// ARR: index=1, value=map[string]interface {}{...}
// OBJ: key="fld2", value="val2"
// OBJ: key="InnerJSON2", value="NoneValue"
Upvotes: 2