I keep getting "unexpected end of JSON input" error when trying to unmarshal a response.
Basically I hit a REST API endpoint, the output result is in "JSON" form but it shows as a string which I then unmarshal. The code I'm using to unmarshal is below. Any suggestions?
func ToJSON(jobs string) map[string]interface{} {
bytes := []byte(jobs)
var result map[string]interface{}
unmarshallError := json.Unmarshal(bytes, &result)
if unmarshallError != nil {
fmt.Println(unmarshallError)
log.Fatal("Error here ")
}
return result
}
**评论:**
Gacnt:
taway6969666:How are you passing jobs to the function. My guess is you're calling
res.Close
on whichever data you're sending to jobs. But without seeing all the code it's just a guess.
Gacnt:So long story short, I'm trying to build a minature SDK for Splunk using the REST API endpoints and Golang.
There are 2 specific endpoints I am focused on. The return values for these endpoints is a JSON like string (e.g its a string but looks like JSON format).
I will then do something like this:
func main() { a, err: = Splunk.EndPointOne(param1,param2,param3) if err != nil { fmt.Println(err.Error())} result := ToJSON(a) searchID := result["sid"].(string) final, errs := Splunk.EndPointTwo(searchID) if errs != nil { fmt.Println(errs.Error()) } fmt.Println(ToJSON(final)) }
In the code sample above, the first "ToJSON" to convert "a" to JSON and save the value into "result" works.
The last "ToJSON" does not work though--it returns the error above (the unexpected end of input).
Both "a" and "final" are technically JSON but in string format when returned. I then convert them into bytes, then unmarshal the JSON which returns as a "map[string]interface{}"
Suggestions?
E: New to go so don't cruificy me for any bad standards/practices--trying to use this project as a way to learn Go :P
taway6969666:What does final print if you log it?
And all good on being new to it. But I would suggest using an editor that uses the gofmt tool to format your code to Go standards. One nice thing about the gofmt tool is it makes reading other code very consistent
Gacnt:It prints as an empty line in the terminal. It seems like this function isn't running properly
taway6969666:Well that's your problem then! Can't unmarshal nothing
Yeah I also found out over on the Splunk forums that my first method is good but I need to add an intermediary before running the second method/endpoint. Apparently there needs to be a check to see if the first method finished on the remote server before I can run the 2nd.
Thanks!