cmd-zet/internal/json/json.go
Oliver Davies 4d11b5354c Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
2025-10-05 00:57:15 +01:00

40 lines
583 B
Go

package json
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
type Item struct {
ID int `json:"id"`
Title string `json:"title"`
}
func AsJSON(zets []string) (string, error) {
var items []Item
for _, entry := range zets {
parts := strings.SplitN(entry, " ", 2)
id, _ := strconv.Atoi(parts[0])
item := Item{
ID: id,
Title: parts[1],
}
items = append(items, item)
}
jsonData, err := json.MarshalIndent(items, "", " ")
if err != nil {
fmt.Println("Error marshaling to JSON:", err)
return "", err
}
return string(jsonData), nil
}