JSON Marshaling not outputting anything

Can anyone tell me why this isn’t working? I’ve cut down my code to a basic example. I’m trying to store several variables in a struct then write the whole struct out as JSON.

package main

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

type DiskSpaceOutput struct {
	text string
	last_updated string
}


func main() {

	var JSONOutput DiskSpaceOutput
	JSONOutput.last_updated = fmt.Sprintf(time.Now().Format(time.RFC3339))
	JSONOutput.text = string("test")

	fmt.Println(JSONOutput.last_updated)

	JSONOutputText, err := json.Marshal(JSONOutput)
	if err != nil {
		log.Fatal(err)
	}
	
	fmt.Println(string(JSONOutputText),err)
}

The output I get is:

2009-11-10T23:00:00Z
{} <nil>

Program exited.

I can’t see why the JSONOutputText is {} rather than showing the data in the struct in JSON format.

Link to the above code at go playground: https://play.golang.org/p/e2f7uArQ71I

JSON does not see I exported fields. You need to export them.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.