You will need to create a custom type and unmarshal method. Plenty of example if you search for "golang custom json unmarshal".
As I've only had to implement it for a couple of simple types I can't offer my own valid example. On Thursday, 14 September 2023 at 14:36:12 UTC+1 Tobias Klausmann wrote: > Hi! > > I am trying to write Prometheus exporter for stats queried fro the Kea > DHCP server. Unfortunatly, the JSON is structured very badly if I want > to use the base library JSON Unmarshal functionality: > > { > "arguments": { > "cumulative-assigned-addresses": [ [ 1, "2023-09-13 12:08:09.597483" ], > ... ], > "pkt4-decline-received": [ [ 0, "2023-09-13 08:01:35.964113" ], ... ], > "subnet[1].assigned-addresses": [ [ 1, "2023-09-13 08:01:36.014127" ], ... > ], > "subnet[1].cumulative-assigned-addresses": [ [ 0, "2023-09-13 > 08:01:36.014006" ], ... ], > "subnet[1].declined-addresses": [ [ 0, "2023-09-13 08:01:36.014069" ], ... > ], > "subnet[1].reclaimed-declined-addresses": [ [ 0, "2023-09-13 > 08:01:36.014074" ], ... ], > "subnet[1].reclaimed-leases": [ [ 0, "2023-09-13 08:01:36.014080" ], ... ], > "subnet[1].total-addresses": [ [ 15, "2023-09-13 08:01:36.013997" ], ... ], > "subnet[1].v4-reservation-conflicts": [ [ 0, "2023-09-13 08:01:36.014010" > ], ... ], > "subnet[2].assigned-addresses": [ [ 4, "2023-09-14 13:32:20.906085" ], ... > ], > "subnet[2].cumulative-assigned-addresses": [ [ 4, "2023-09-14 > 13:32:20.906090" ], ... ], > "subnet[2].declined-addresses": [ [ 0, "2023-09-13 08:01:36.014088" ], ... > ], > "subnet[2].reclaimed-declined-addresses": [ [ 0, "2023-09-13 > 08:01:36.014096" ], ... ], > "subnet[2].reclaimed-leases": [ [ 3, "2023-09-14 00:08:10.270122" ], ... ], > "subnet[2].total-addresses": [ [ 223, "2023-09-13 08:01:36.014015" ], ... > ], > "subnet[2].v4-reservation-conflicts": [ [ 0, "2023-09-13 08:01:36.014025" > ], ... ], > "subnet[3].assigned-addresses": [ [ 1, "2023-09-13 08:01:36.014135" ], ... > ] > ... rest of subnet[3] and more subnets > }, > "result": 0 > } > > > The int, timestamp lists are already Not Great, but I can deal with > those. > > The problem is the series of subnet[x] fields. They vary depending on > how many subnets the server serves, and nothing in the JSON indicates > how many there are. And even if it did: getting the stdlib JSON > Unmarshaler to actually pick them up (without hardcoded struct tags) > seems impossible, short of essentially writing my own JSON Unmarshaler > from scratch. > > So I have three questions: > > 1. Am I missing some wildcard-ish functionality where I can tell the > stdlib JSON Unmarshaler just make a slice out of all the JSON > elements that fit a pattern? > 2. Is there a Golang JSON library that is better suited to dealing with > this? > 3. What other options do I have (besides "use another > language/exporter", "make upstream produce better JSON" and "write > your own parser")? > > Best & TIA, > Tobias > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5e8cb3d6-7a46-48ca-a973-4f426fcb1be6n%40googlegroups.com.