go icon indicating copy to clipboard operation
go copied to clipboard

Examples on reading objects from nested JSON

Open nilesh-akhade opened this issue 2 years ago • 1 comments

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
}

nilesh-akhade avatar Sep 28 '22 12:09 nilesh-akhade

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?

nilesh-akhade avatar Sep 28 '22 12:09 nilesh-akhade