Nurzhan Nogerbek
Nurzhan Nogerbek

Reputation: 5236

How to make nested JSON response in Go?

I am new in Go and need some help.

In my PostgreSQL database I have 4 table. They called: surveys, questions, options and surveys_questions_options.

They looks like this:

surveys table:

| survey_id (uuid4)                    | survey_name (varchar) |
|--------------------------------------|-----------------------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | April                 |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | May                   |

questions table:

| question_id (int) | question_text (text)         |
|-------------------|------------------------------|
| 1                 | What is your favorite color? |

options table:

| option_id (int)   | option_text (text) |
|-------------------|--------------------|
| 1                 | red                |
| 2                 | blue               |
| 3                 | grey               |
| 4                 | green              |
| 5                 | brown              |

surveys_questions_options table combines data from all three previous tables:

| survey_id                            | question_id | option_id |
|--------------------------------------|-------------|-----------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 1         |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 2         |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 3         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 3         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 4         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 5         |

How can I make nested JSON response in Go? I use GORM library. I want a JSON response like this:

[
    {
        "survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
        "survey_name": "April",
        "questions": [
            {
                "question_id": 1,
                "question_text": "What is your favorite color?",
                "options": [
                    {
                        "option_id": 1,
                        "option_text": "red"
                    },
                    {
                        "option_id": 2,
                        "option_text": "blue"
                    },
                    {
                        "option_id": 3,
                        "option_text": "grey"
                    },
                ]
            }
        ]
    },
    {
        "survey_id": "b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720",
        "survey_name": "May",
        "questions": [
            {
                "question_id": 1,
                "question_text": "What is your favorite color?",
                "options": [
                    {
                        "option_id": 3,
                        "option_text": "grey"
                    },
                    {
                        "option_id": 4,
                        "option_text": "green"
                    },
                    {
                        "option_id": 5,
                        "option_text": "brown"
                    },
                ]
            }
        ]
    }
]

My models looks like this:

type Survey struct {
  SurveyID string `gorm:"primary_key" json:"survey_id"`
  SurveyName string `gorm:"not null" json:"survey_name"`
  Questions []Question 
}

type Question struct {
  QuestionID int `gorm:"primary_key" json:"question_id"`
  QuestionText string `gorm:"not null;unique" json:"question_text"`
  Options []Option
}

type Option struct {
  OptionID   int    `gorm:"primary_key" json:"option_id"`
  OptionText string `gorm:"not null;unique" json:"option_text"`
}

Upvotes: 0

Views: 810

Answers (3)

Narendranath Reddy
Narendranath Reddy

Reputation: 4133

However, try this: To fetch nested data in m2m relation

type Survey struct {
  gorm.Model
  SurveyID string       `gorm:"primary_key" json:"survey_id"`
  SurveyName string     `gorm:"not null" json:"survey_name"`
  Questions []*Question `gorm:"many2many:survey_questions;"`
}

surveys := []*model.Survey{}
db := dbSession.Where(&model.Survey{SurveyID: id}).Preload("Questions").Find(&surveys)

Upvotes: 0

Alon
Alon

Reputation: 107

We're missing some scope from your code, and so it's quite hard to point you in the right direction. Are you asking about querying GORM so you get []Survey, or are you asking about marshalling []Survey? Anyway, you should add the tag to Questions too, as slomek replied.

Upvotes: 1

slomek
slomek

Reputation: 5126

I'm not sure abour GORM part, but with JSON you need to add struct tags on the nested objects as well:

type Survey struct {
  ...
  Questions []Question `json:"questions"`
}

type Question struct {
  ...
  Options []Option `json:"options"`
}

Upvotes: 1

Related Questions