ProgramGuru

JSON-Based Task Manager

Start with JSON-Based Task Manager

Learn json-based task manager through an executable example, then connect the small demonstration to production code.

What you’ll learn

  • Explain the main purpose of json-based task manager.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

A JSON task manager models tasks as structs and encodes them for persistence or network exchange. Keep state changes separate from serialization, and validate decoded fields before use.

This example performs an in-memory JSON round trip so it does not depend on a local file.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for json task managerJSON is a representation; persistence and validation are separate.Task valuesStep 1Encode JSONStep 2Persist or sendStep 3Decode valuesStep 4JSON is a representation; persistence and validation are separate.
JSON is a representation; persistence and validation are separate.

Tools, syntax and practical notes

Expansion: add task IDs, validation, add/list/done/delete commands and a save-to-temp-file-then-rename strategy. For concurrent access, introduce a lock or transactional store.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"encoding/json"
	"fmt"
)

type Task struct {
	ID    int    `json:"id"`
	Title string `json:"title"`
	Done  bool   `json:"done"`
}

func main() {
	tasks := []Task{{ID: 1, Title: "Study Go", Done: false}, {ID: 2, Title: "Write tests", Done: true}}
	encoded, err := json.Marshal(tasks)
	if err != nil {
		panic(err)
	}
	var restored []Task
	if err := json.Unmarshal(encoded, &restored); err != nil {
		panic(err)
	}
	fmt.Println("JSON:", string(encoded))
	fmt.Println("Restored tasks:", len(restored))
}

Output

JSON: [{"id":1,"title":"Study Go","done":false},{"id":2,"title":"Write tests","done":true}]
Restored tasks: 2

Trace what the program does

json.Marshal produces an array with named JSON fields. Unmarshal reconstructs two Task values with the same exported fields.

Avoid common mistakes

  • Use exported struct fields when JSON encoding needs to read them.
  • For file persistence, avoid partially overwriting the only copy of important data; consider atomic write strategies.

Try it yourself

Add a task completion update and persist tasks using os.WriteFile in a temporary directory.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why are the Task fields exported?
Choose one answer. You can try again.

Answer and explanation

encoding/json needs access to fields it encodes

Correct. json.

What you should remember

You have practiced JSON-Based Task Manager in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Command-Line To-Do Application →