3

I am importing "encoding/json" package to print output in json.I was able to get the json format but i want to modify the response structure. Here is the sample code

var people []Person
people = append(people, Person{Id: "1", Firstname: "John", Lastname: "Doe", Address: Address{City: "City X", State: "State X"}})
people = append(people, Person{Id: "2", Firstname: "Koko", Lastname: "Doe", Address: Address{City: "City Z", State: "State Y"}})
people = append(people, Person{Id: "3", Firstname: "Francis", Lastname: "Sunday"})
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(people)

which outputs the following response

[
{
    "id": "1",
    "firstname": "John",
    "lastname": "Doe",
    "address": {
        "city": "City X",
        "state": "State X"
    }
},
{
    "id": "2",
    "firstname": "Koko",
    "lastname": "Doe",
    "address": {
        "city": "City Z",
        "state": "State Y"
    }
},
{
    "id": "3",
    "firstname": "Francis",
    "lastname": "Sunday",
    "address": {
        "city": "",
        "state": ""
    }
}
]

But i want to response into the following format

{
"status": "200",
"data": [
    {
        "id": "1",
        "firstname": "John",
        "lastname": "Doe",
        "address": {
            "city": "City X",
            "state": "State X"
        }
    },
    {
        "id": "2",
        "firstname": "Koko",
        "lastname": "Doe",
        "address": {
            "city": "City Z",
            "state": "State Y"
        }
    },
    {
        "id": "3",
        "firstname": "Francis",
        "lastname": "Sunday",
        "address": {
            "city": "",
            "state": ""
        }
    }
        ]
}

How can I modify the response format ? I am beginner in Go. Thanks for your help.

2
  • 2
    What did you try? What doesn't work for you? Commented Jan 29, 2018 at 10:35
  • 1
    I want to reorganize json response format.Since i am new in go i was not able to reformat and sadly I was not able to found in internet . Commented Jan 29, 2018 at 10:44

2 Answers 2

5

In order to change structure of your response, you need to provide the structure that you want to achieve with type.

You can define Response struct type which will consist of data and status fields:

type Response struct {
    Status string `json:"status"`
    Data []Person `json:"data"`
}

var people []Person
people = append(people, Person{Id: "1", Firstname: "John", Lastname: "Doe", Address: Address{City: "City X", State: "State X"}})
people = append(people, Person{Id: "2", Firstname: "Koko", Lastname: "Doe", Address: Address{City: "City Z", State: "State Y"}})
people = append(people, Person{Id: "3", Firstname: "Francis", Lastname: "Sunday"})
w.Header().Set("Content-Type", "application/json")

resp := Response{Status: "200", Data: people}
json.NewEncoder(w).Encode(resp)
Sign up to request clarification or add additional context in comments.

Comments

1

you just need to pass response and request object to the function you are using w.Header().Set("Content-Type", "application/json"). Either you can append to the struct or you can initialize it with multiple values in an array.

package main

import "encoding/json"

//  Data struct
type Data struct {
    status string
    data   []Person
}

// Person struct
type Person struct {
    Id        string
    Firstname string
    Lastname  string
    Address   Address
}

// Address struct
type Address struct {
    City  string
    State string
}

func main() {
    people := Data{
        status: "200",
        data: []Person{
            {Id: "1", Firstname: "John", Lastname: "Doe", Address: Address{City: "City X", State: "State X"}},
            {Id: "2", Firstname: "Koko", Lastname: "Doe", Address: Address{City: "City Z", State: "State Y"}},
            {Id: "3", Firstname: "Francis", Lastname: "Sunday", Address: Address{City: "City Z", State: "State Y"}},
        },
    }
    json.NewEncoder(w).Encode(people)
}

1 Comment

This is also another best solution for my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.