hi , I am also having the same problem, can you please elaborate
invalid operation: dat["connections"]["current"] (type interface {} does not support indexing) Thanks On Friday, August 21, 2015 at 4:22:22 PM UTC+5:30, Giulio Iotti wrote: > > On Friday, August 21, 2015 at 11:51:32 AM UTC+2, Victor Hooi wrote: >> >> [...] >> However, the following will not work: >> >> fmt.Println(dat["connections"]["current"]) >> >> >> I get: >> >> >> ./parse_serverstatus.go:37: invalid operation: >>> dat["connections"]["current"] (type interface {} does not support indexing) >> >> >> The thing is, not every field is nested to to the same depth. >> >> >> Question 1 - Is there a way to unmarshall the above JSON, and have Go >> "magically" handle the types, and still allow me to access them afterwards >> as a normal map? >> > > No, there is no magic. But there is a pretty neat syntax, a type switch: > https://golang.org/doc/effective_go.html#type_switch > > So in your case, asset that dat["connections"] is a map; it is, get > "current", otherwise... > > >> (For context, in Python, I'm just using "server_status_json = >> json.loads(line)", and it all pretty much works as you'd expect). >> > > Yes, but Python has a different view of types ;) > > >> Question 2 - Is there a way to store a mapping of names, to locations, >> such that I can access metrics easily? >> >> >> For example, in the Python version I have: >> >> >> def get_nested_items(obj, *names): >>> """Return obj[name][name2] ... [nameN] for any list of names.""" >>> for name in names: >>> obj = obj[name] >>> return obj >>> metrics_to_extract = { >>> 'available_connections': ['connections', 'available'], >>> 'current_connections': ['connections', 'current'] >>> } >>> for key, names in metrics_to_extract.items(): >>> value = get_nested_items(server_status_json, *names) >>> json_points.append(create_point(key, value, timestamp, tags)) >> >> >> In this case, metrics_to_extract has the name and locations, and I just >> iterate over that and add them to my output (json_points). >> >> (Originally I was using locations like >> "server_status_json['connections']['current']" along with eval(), which >> wasn't the best). >> >> What's the idiomatic way of doing this in Go? >> > > Wherever you have a map of constant keys in Python, make a struct in Go. > Then you can have a slice of structs (pointers). > > >> Question 3 - One other complication is that sometimes, some of the float >> fields get wrapped in a "floatApprox" - e.g.: >> >> "connections": { >>> "current": 131, >>> "available": 51069, >>> "totalCreated": { >>> "floatApprox": 177400 >>> } >>> }, >> >> >> What's a clean way of handling this? Would you just try and access the >> location, and if it failed, try again but adding one level of floatApprox >> nesting - does that sound reasonable? >> > > As above, you need type assertions. Ask again if you want examples of more > clarifications! > > -- > Giulio > https://twitter.com/dullboy > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.