[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-go-json-all--*":3,"academy-blog-translations-cyp5xcm5e0h680m":84},{"data":4,"page":72,"perPage":72,"totalItems":72,"totalPages":72},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":78,"keywords":79,"locale":54,"published_at":80,"scheduled_at":13,"school_blog":76,"short_description":81,"slug":82,"status":74,"title":6,"updated":83,"updated_by":13,"views":77},"EP.13 Go and JSON - Making Data Conversion Easy!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go and JSON - Making Data Conversion Easy!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>Do you want your program's data to be easy to read and compatible with other applications? JSON is the answer! Today, we will teach you how to convert data to JSON and back to use in Go easily.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>What is JSON?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy to read and write. It is commonly used for communicating and exchanging data between different programs, especially in applications that connect to APIs or servers.\u003C\u002Fp>\u003Cp>\u003Cstrong>Structure of JSON\u003C\u002Fstrong>\u003Cbr>JSON stores data in the form of \"name\": value pairs, for example:\u003C\u002Fp>\u003Cp>In this example:\u003Cbr>\"name\": \"Alice\" is a string.\u003Cbr>\"age\": 25 is a number.\u003Cbr>\"isStudent\": true is a boolean value.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">{\r\n  \"name\": \"Alice\",\r\n  \"age\": 25,\r\n  \"isStudent\": true\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Converting Data to JSON in Go (Encoding)\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Converting data in Go to JSON is straightforward using the json.Marshal function with a struct to hold the data we want to convert.\u003Cbr>Example:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"encoding\u002Fjson\"\r\n    \"fmt\"\r\n)\r\n\r\ntype Person struct {\r\n    Name string `json:\"name\"`\r\n    Age  int    `json:\"age\"`\r\n}\r\n\r\nfunc main() {\r\n    p := Person{Name: \"Alice\", Age: 25}\r\n    jsonData, err := json.Marshal(p)\r\n    if err != nil {\r\n        fmt.Println(\"Error encoding JSON:\", err)\r\n        return\r\n    }\r\n    fmt.Println(string(jsonData)) \u002F\u002F แสดงผล JSON เป็น string\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In this example:\u003Cbr>We create a struct called Person with fields Name and Age.\u003Cbr>We use json.Marshal(p) to convert Person to JSON.\u003Cbr>The result will be:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">{\"name\":\"Alice\",\"age\":25}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Converting JSON Back to Data in Go (Decoding)\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>When we have JSON and want to convert it back to a struct, we use json.Unmarshal.\u003C\u002Fp>\u003Cp>In this example: We convert JSON (jsonData) back to a struct named Person using json.Unmarshal.\u003Cbr>The result will display the name and age of the Person.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"encoding\u002Fjson\"\r\n    \"fmt\"\r\n)\r\n\r\ntype Person struct {\r\n    Name string `json:\"name\"`\r\n    Age  int    `json:\"age\"`\r\n}\r\n\r\nfunc main() {\r\n    jsonData := []byte(`{\"name\":\"Alice\",\"age\":25}`)\r\n    var p Person\r\n    err := json.Unmarshal(jsonData, &amp;p)\r\n    if err != nil {\r\n        fmt.Println(\"Error decoding JSON:\", err)\r\n        return\r\n    }\r\n    fmt.Println(p.Name, \"is\", p.Age, \"years old.\")\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Summary\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Use json.Marshal to convert data to JSON (Encoding).\u003Cbr>Use json.Unmarshal to convert JSON back to data (Decoding).\u003Cbr>JSON makes data easy to read and convenient for use with other applications.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>You can try creating a struct with various fields such as name, age, and student status, then convert it to JSON and back to a struct to see if the data remains the same!\u003C\u002Fp>","28_11zon_k8kx2brlip.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fyuu1ktudugg2u96\u002F28_11zon_k8kx2brlip.webp","2026-03-04 08:51:49.290Z","",{"keywords":15,"locale":48,"school_blog":58},[16,23,28,33,38,43],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:51:48.630Z","8gj85u1w4mpmcsw","data handling","2026-04-10 16:14:40.188Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:51:48.853Z","rl3x8ognoa2hzrb","Unmarshal","2026-04-10 16:14:40.365Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:51:49.059Z","4mugqcb1gl9joc6","Marshal","2026-04-10 16:14:40.504Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:46:24.109Z","o8xfgwdh6k03hxd","JSON","2026-04-10 16:13:13.679Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":49,"collectionId":50,"collectionName":51,"created":52,"flag":53,"id":54,"is_default":55,"label":56,"updated":57},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":59,"collectionId":60,"collectionName":61,"expand":62,"id":76,"views":77},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":63},{"blogIds":64,"collectionId":65,"collectionName":66,"created":67,"created_by":13,"id":59,"image":68,"image_alt":13,"image_path":69,"label":70,"name":71,"priority":72,"publish_at":73,"scheduled_at":13,"status":74,"updated":75,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":71,"th":71},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","cyp5xcm5e0h680m",209,"yuu1ktudugg2u96",[20,25,30,35,40,45],"2025-01-30 06:01:34.482Z","Learn how to convert data to JSON using Marshal and back to data using Unmarshal in Go.","go-json","2026-04-22 07:11:50.067Z",{"en":82}]