Friday, October 10, 2025
HomeGolangJson information not displaying - Code Overview

Json information not displaying – Code Overview


Hello,
Once I GET the info from the next operate, I can get it printed out utilizing fmt.Println however it within the browser and within the postman I get following.
[
{},
{},
{}
]
The operate is the next.

func sendResponse(w http.ResponseWriter, statusCode int, payload interface{}){

fmt.Println(“Payload Unmarshal “,payload)
// prints this >> Payload Unmarshal [{1 Java} {2 python} {3 golang}]

response, _ := json.Marshal(payload)

w.Header().Set(“Content material-Sort”,“software/json; charset=utf-8”)

w.WriteHeader(statusCode)

fmt.Println(“From sendResponse app “,response)
// Prints this >> From sendResponse app [91 123 125 44 123 125 44 123 125 93]

w.Write(response)

}
Can anybody assist me on this regard, what unsuitable is finished within the code ?

Very first thing I see: you could possibly be swallowing an error. Change this:

// From this...
response, _ := json.Marshal(payload)
// ... to this:
response, err := json.Marshal(payload)
if err != nil {
	fmt.Println("error:", err)
}

If you wish to see what the json.Marshal’d string truly appears to be like like you could possibly additionally change this line:

// From this...
fmt.Println("From sendResponse app ",response)
// ... to this:
fmt.Println("From sendResponse app ", string(response))

Strive that and tell us what you discover.

Thanks for the reply, however It didn’t assist :woozy_face:

fmt.Println("From sendResponse app ",response)
// Payload Unmarshal  [{1 Java} {2 python} {3 golang}]
fmt.Println("From sendResponse app ", string(response))
//  From sendResponse app  [{},{},{}]

Invalid JSON!??

Error: Parse error on line 1: [{1 Java} {2 python} { –^ Expecting ‘STRING’, ‘}’, got ‘NUMBER’

Nopes, this (Invalid JSON) is not the case !

What’s the type on payload? I suspect your problem is coming from code that isn’t visible here. Consider the following:

package main

import (
	"encoding/json"
	"fmt"
)

type Payload struct {
	ID    int
	Value string
}

func main() {
	payload := []Payload{{1, "Java"}, {2, "python"}, {3, "golang"}}
	fmt.Println(payload)
	response, err := json.Marshal(payload)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Println(string(response))
}

… which produces:

[{1 Java} {2 python} {3 golang}]
[{"ID":1,"Value":"Java"},{"ID":2,"Value":"python"},{"ID":3,"Value":"golang"}]

You may run it your self within the playground. For those who can tweak this to offer a reproducible instance any person right here may have the ability that will help you.

Thanks for replying and to your consideration. The code I’m engaged on is right here.
All what you need to do with the intention to run the appliance is to run the docker-compose up and entry the localhost:8080/languages to get all of the languages.

Didn’t have to run it. Already discovered the issue:

sort language struct {
	id int `json:"id"`
	identify string `json:"identify"`
}

These fields aren’t exported. Change this to:

sort language struct {
	ID int `json:"id"`
	Title string `json:"identify"`
}



1 Like

Bundle of thanks, it really works now !
I didn’t take note of that :frowning:

Thanks as soon as once more :slight_smile:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments