How to join HTTP JSON responses

Hello,

In a for loop in Golang that does HTTP GET requests to a group of links, what would be the best approach to joining the output of each HTTP JSON response under one single array object?

for i := 0; i < len(servers.Sites); i++ {
    		req, err := http.NewRequest(http.MethodGet, servers.Sites[i].URLs, nil)

    		if err != nil {
    			log.Println(err)
    		}
    }

I would like to view the responses joined under one object, like this, under the “vm” object:

output := Output{
            Success:   true,
            Vm:        <JSON outputs here, each separated by comma>
        }

Does this do what you want? Based on the error I get on the Go playground, it looks like that environment isn’t configured to make network requests, so I’m not sure if it’s accurate or not.

https://play.golang.org/p/ZghU8fuV3Jf

1 Like

Thanks Sean. I tried it on my laptop Go’s engine and got:

{true [invalid character '<' looking for beginning of value]}

Have you swapped google.com by an URL that does actually send you some JSON?

1 Like

yes. it worked with your suggestions. thanks for the help!

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