hello folks,
I have a json file and I am accessing the same and trying to convert to struct using json.Unmarshal while doing so I have nested key value pairs in the json. How do I access nested map specifically the “tables” the non nested is straight forward. Please help
My json looks like
cat http1.json
{
“deviceGroup”: “none”,
“fullPath”: “/Common/top”,
“inheritedDevicegroup”: “true”,
“inheritedTrafficGroup”: “true”,
“tables”: [
{
“name”: “basic__snatpool_members”
},
{
“name”: “net__snatpool_members”
},
{
“name”: “optimizations__hosts”
},
{
“columnNames”: [
“name”
],
}
========function here
func dataToIapp(name string, d *schema.ResourceData) bigip.Iapp {
var p bigip.Iapp
var obj map[string]interface{}
jsonblob := []byte(d.Get("jsonfile").(string))
err := json.Unmarshal(jsonblob, &obj)
if err != nil {
fmt.Println("error", err)
}
log.Printf("value of +++++++++++++++++++++++++++++all objects %+v\n p ", obj)
p.Name = obj[“name”].(string)
p.Partition = obj[“partition”].(string)
p.Devicegroup = obj[“deviceGroup”].(string)
p.InheritedDevicegroup = obj[“inheritedDevicegroup”].(string)
p.Tables = obj[“tables”].(string)
when I compile I get this error
go build
bigip/resource_bigip_sys_iapp.go:319:11: cannot use obj[“tables”].(string) (type string) as type []bigip.Tablesrecord in assignment
my struct is as below
package bigip
import (
“encoding/json”
//“log”
)
// LIC contains device license for BIG-IP system.
type Iapps struct {
Iapps []Iapp json:"items"
}
type Iapp struct {
Name string
Partition string
Description string
Devicegroup string
ExecuteAction string
InheritedDevicegroup string
StrictUpdates string
Template string
TemplateModified string
TemplatePrerequisiteErrors string
TrafficGroup string
InheritedTrafficGroup string
Lists []Listsrecord
Metadata []Metadatarecord
Tables []Tablesrecord
Variables []Variablesrecord
Jsonfile string
}
type iappDTO struct {
Name string json:"name,omitempty"
Partition string json:"partition,omitempty"
Description string json:"description,omitempty"
Devicegroup string json:"deviceGroup,omitempty"
ExecuteAction string json:"executeAction,omitempty"
InheritedDevicegroup string json:"inheritedDevicegroup,omitempty"
InheritedTrafficGroup string json:"inheritedTrafficGroup,omitempty"
StrictUpdates string json:"strictUpdates,omitempty"
Template string json:"template,omitempty"
TemplateModified string json:"templateModified,omitempty"
TemplatePrerequisiteErrors string json:"templatePrerequisiteErrors,omitempty"
TrafficGroup string json:"trafficGroup,omitempty"
Jsonfile string json:"apiAnonymous,omitempty"
Lists struct {
Items []Listsrecord `json:"items,omitempty"`
} `json:"listsReference,omitempty"`
Metadata struct {
Items []Metadatarecord `json:"items,omitempty"`
} `json:"metadataReference,omitempty"`
Tables struct {
Items []Tablesrecord `json:"items,omitempty"`
} `json:"tablesReference,omitempty"`
Variables struct {
Items []Variablesrecord `json:"items,omitempty"`
} `json:"variablesReference,omitempty"`
}
type Listsrecord struct {
Encrypted string json:"encrypted"
Value string json:"value"
}
type Metadatarecord struct {
Persist string json:"persist"
Value string json:"value"
}
type Tablesrecord struct {
ColumnNames string json:"columnNames"
Name string json:"name"
EncryptedColumns string json:"encryptedColumns"
Rows string json:"rows"
}
type Variablesrecord struct {
Encrypted string json:"encrypted"
Name string json:"name"
Value string json:"value"
}