thedevsaddam/gojsonq

How access array of objects using gojsonq?

jaylahoti opened this issue · 1 comments

const json = [{"city":"dhaka","type":"weekly","temperatures":[30,39.9,35.4,33.5,31.6,33.2,30.7]}]

avg := gojsonq.New().JSONString(json).From("[0].temperatures").Avg()
Above code does not work.

How can it be done?

This should work

package main

import (
	"fmt"

	"github.com/thedevsaddam/gojsonq"
)

func main() {
	const json = `[{"city":"dhaka","type":"weekly","temperatures":[30,39.9,35.4,33.5,31.6,33.2,30.7]}]`
	result := gojsonq.New().FromString(json).From("[0].temperatures").Avg() // handle error
	fmt.Printf("%#v\n", result)
}

//output: 33.471428571428575

Golang play link: https://play.golang.org/p/YS2sUm_DHv3