How to make a json response that looks like this

I am trying to create this output when calling my GOlang API.

{
    "jsonrpc": "2.0",
    "result": [
        false,
        "",
        "",
        ""
    ],
    "id": 1
}

So far, using

type JSONRpc struct {
	Id      json.RawMessage `json:"id,omitempty"`
	Version string          `json:"jsonrpc,omitempty"`
	Result  interface{}    `json:"result"`
}


message := JSONRpc{Id: id, Version: "2.0", Result: fmt.Sprintf("[%t, %q, %q, %q]", false, "", "", "")}
Encode(&message)

and

[%t, %s, %s, %s]

I am able to get

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": "[false, \"\", \"\", \"\"]"
}

and

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": "[false, , , ]"
}

How do I get a perfect

{
    "jsonrpc": "2.0",
    "result": [
        false,
        "",
        "",
        ""
    ],
    "id": 1
}

Don’t set the result to a string:

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

1 Like

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