-2
package main

import (
    "encoding/json"
    "fmt"
    "log"
)

func main() {
    type FruitBasket struct {
        formatVersion    string `json:"formatVersion"`
        terraformVersion string `json:"terraformVersion"`
    }

    jsonData := []byte(`{"formatVersion":"0.1","terraformVersion":"0.13.5"}`)

    var basket FruitBasket

    err := json.Unmarshal(jsonData, &basket)
    if err != nil {
        log.Println(err)
    }

    fmt.Println(basket.formatVersion, basket.terraformVersion)
}

I have a struct defined to match JSON structure. I am trying to unmarshal JSON object to the struct type. but I am not getting anything in my struct var.

There are no syntax errors. Can you tell me where I went wrong?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

1

Only exported fields are marshalled and unmarshalled.

Changing the type to

type FruitBasket struct {
    FormatVersion    string `json:"formatVersion"`
    TerraformVersion string `json:"terraformVersion"`
}

will give you the expected results.

super
  • 12,335
  • 2
  • 19
  • 29