go
go copied to clipboard
Examples on reading objects from nested JSON
We are trying to use this library for a use case where we need to iterate over nested types/json objects. We couldn't find any example of how to do it. Any help? I would be happy to contribute examples/docs.
Input
Assume there are millions of baskets and millions of fruits in each. Hence we cannot use json.Unmarshal()
, it will cause an out-of-memory error.
{
"baskets": [{
"id": "1",
"fruits": [{
"color": "red",
"name": "apple"
}, {
"color": "green",
"name": "apple"
}, {
"color": "yellow",
"name": "banana"
}, {
"color": "orange",
"name": "orange"
}]
}]
}
Output Type
type Fruit struct {
Name string
Color string
BasketID string // from parent type
}
Docs says,
Besides, jsoniter.Iterator provides a different set of interfaces iterating given bytes/string/reader and yielding parsed elements one by one. This set of interfaces reads input as required and gives better performance.
But how?