Ankita
Ankita

Reputation: 165

JSON Response: return nested JSON

I am trying to return a JSON response something like this:

c.JSON(http.StatusOK, gin.H{"data": resp, "code": http.StatusOK, "status": "success"})

where resp contains data from a db table (struct) which I have converted to JSON.

I need to return the response in data key in this format:

data["result"] = resp

Sample response should look like:

{
"data": {"result" : ["This is a sample response"]}
}

The response can either be an object or a list of objects. This is in Python format, how do I do this in Go?

Upvotes: 3

Views: 2525

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

You could see it in the source of gin:

type H map[string]interface{}

So you could use(nested gin.H):

c.JSON(http.StatusOK, gin.H{"data": 
        gin.H{
            "result": []string{"This is a sample response"},
        },
        "code": http.StatusOK, 
        "status": "success",
    })

Upvotes: 7

Related Questions