Initializing nested struct, by dynamically forming struct member path

741 views
Skip to first unread message

bharath...@gmail.com

unread,
Feb 18, 2017, 10:04:47 AM2/18/17
to golang-nuts
Hi,

I am learning Go language and also to technical terms like in subject "struct member path" (don't know if right).

I need help to initialize a nested structure like below, by getting the struct member names at runtime.

type jsonData struct {
    DataReference []struct {
        ParameterType string
        Applicationtype struct {
            Application1 struct {
                ApplicationName string
Param1 struct {
Name string
}
Param2 struct {
Name string
}
}
Application2 struct {
ApplicationName string
Param1 struct {
Name string
}
Param2 struct {
Name string
}
}
}
}
}

I want to form the "struct member path" jsonData.DataReference[0].Applicationtype.Application1.Param1.Name at runtime where I will get the value "1" as index in "Application1" & "Param1" at runtime by iterating the obtained JSON when using GOs range.

And similarly based on the index, will initialize Application1.Param2.Name, Application2.Param1.Name

It would be of great help, if someone could help me here.

Thanks in advance.

Regards,
Bharath B

Matt Harden

unread,
Feb 18, 2017, 6:06:02 PM2/18/17
to bharath...@gmail.com, golang-nuts
Why do you want to to this? What are you actually trying to accomplish? See http://xyproblem.info/

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

roger peppe

unread,
Feb 18, 2017, 7:00:22 PM2/18/17
to bharath...@gmail.com, golang-nuts
It sounds to me like the encoding/json package in the standard library might do what you want.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

Bharath B

unread,
Feb 19, 2017, 2:27:32 AM2/19/17
to golang-nuts, bharath...@gmail.com
Hi,

I am trying to read those values and store in DB.

Regards,
Bharath B

Nathan Kerr

unread,
Feb 21, 2017, 5:14:09 AM2/21/17
to golang-nuts, bharath...@gmail.com
Could you give an example of the json you are starting from?

Also, what database are you using?

Bharath B

unread,
Feb 22, 2017, 3:08:00 AM2/22/17
to golang-nuts, bharath...@gmail.com
Hi,

Thanks for the response.

Below is the sample json being used,

{
   "jsondata": [
    {
      "dataReference":[
      {
        "parameterType" : "common",
        "applicationtype":
        {
          "application1": 
          {
            "applicationName": "appl1",
            "param1": 
            {
              "name": "param1",
              "value": "test2",
              "Type": "text",
              "oper": "ADD"
            }
          }
        }
      }
      ]
    }
    ]
}

From my side I will populate the struct and provide it the application for consumption.

Regards,
Bharath B

C Banning

unread,
Feb 22, 2017, 6:31:47 AM2/22/17
to golang-nuts, bharath...@gmail.com
Perhaps not very elegant but seems to produce what you're looking for: https://play.golang.org/p/bpM69Q-ddV 

C Banning

unread,
Feb 22, 2017, 4:29:32 PM2/22/17
to golang-nuts, bharath...@gmail.com

Nathan Kerr

unread,
Feb 23, 2017, 5:38:06 AM2/23/17
to golang-nuts, bharath...@gmail.com
You gave two different things you want to accomplish:

1. “I am trying to read those values and store in DB”
2. “I will populate the struct and provide it the application for consumption”

Fortunately accomplishing either or both of these goals follows a similar process.

Start by figuring out what the output should be. In the case of a DB, the schema the data will be put into. For providing it to the application, the data structures you want. You mentioned using range, which means the data you want to range over needs to be in a slice or a map.

Then extract and transform the input into the output.

Some guesses I have made about your data:

- There are a variable number of applications and their names aren’t known before you get the data.
- Applications have different parameters
- Parameter types default to the contents of “parameterType”, in this case “common”

Assuming these guesses are correct, I would unmarshal the json into:

struct {
JsonData []struct {
DataReference []struct {
ParameterType   string `json:"parameterType"`
ApplicationType map[string]map[string]interface{}
} `json:"dataReference"`
} `json:"jsondata"`
}

This sets out the known portions and leaves the variable portions for later handling.

Then iterate over ApplicationType to get the different applications and their parameters. Populate the database or desired data structures during the loop.

An example of this process: https://play.golang.org/p/TNYur-Lr2Z 
Reply all
Reply to author
Forward
0 new messages