Challenge 21 - JSON and Stock Dividends

👋 Welcome Gophers! In this Go challenge, you are going to be working with JSON data and implementing a function that takes in a JSON string full of stock quotes and returns the one with the highest dividend return.

You will need to implement the HighestDividend function so that it takes in a JSON string. You will then need to somehow convert this JSON string into something you can traverse in Go using the encoding/json package.

Finally, you will need to return the string ticker for the stock that has the highest dividend.


  {"ticker": "APPL", "dividend": 0.5},
  {"ticker": "MSFT", "dividend": 0.2}

## HighestDividend returns "APPL"
View Solution

package main

import (

type Stocks struct {
  Stocks []Stocks

type Stock struct {
  Ticker   string 	`json:"ticker"`
  Dividend float64    `json:'dividend"`

// HighestDividend iterates through a JSON string of stocks
// unmarshalls them into a struct and returns the Stock with
// the highest dividend
func HighestDividend(str string) string {
  var ticker []Stock
  if err := json.Unmarshal([]byte(str), &ticker); err != nil {
    return ""
  if len(ticker) == 0 {
    return ""
  highest := ticker[0]
  for _, v:= range ticker {
    if highest.Dividend < v.Dividend {
     	highest = v 
  return highest.Ticker

func main() {
  fmt.Println("Stock Price AI")

  stocks_json := `[

  highestDividend := HighestDividend(stocks_json)

Further Reading:

If you enjoyed this challenge, you may also enjoy some of these other challenges:

Other Challenges