unpacking map[string]interface{} answer

167 views
Skip to first unread message

natxo....@gmail.com

unread,
May 30, 2021, 6:47:36 PM5/30/21
to golang-nuts
hi,

I am struggling to understand how to use this map[string]interface{}.

snippet:

package main

import "fmt"
import "github.com/mattwilmott/go-foreman"

func main() {

    client := foreman.Client("foreman.l.example.org", "admin", "whatever", false, "")
    fmt.Println(client)
    resp, err := client.Get("hosts")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(resp)

}
Foreman GET: map[page:1 per_page:20 results:[map[architecture_id:1 architecture_name:x86_64 build:false capabilities:[build] certname:foreman.l.example.org comment:<nil> compute_profile_id:<nil> compute_profile_name:<nil> compute_resource_id:<nil> compute_resource_name:<nil> configuration_status:0 configuration_status_label:Out of sync created_at:2021-05-30 20:54:20 UTC disk:<nil> domain_id:1 domain_name:l.example.org enabled:true environment_id:1 environment_name:production global_status:0 global_status_label:Warning hostgroup_id:<nil> hostgroup_name:<nil> hostgroup_title:<nil> id:1 image_file: image_id:<nil> image_name:<nil> installed_at:<nil> ip:<nil> ip6:2001:470:7b79:100::44e last_compile:<nil> last_report:2021-05-30 20:54:15 UTC location_id:2 location_name:Default Location mac:ba:a6:69:6e:31:6c managed:false medium_id:<nil> medium_name:<nil> model_id:1 model_name:Latitude E7250 name:foreman.l.example.org operatingsystem_id:1 operatingsystem_name:CentOS 7.9.2009 organization_id:1 organization_name:Default Organization owner_id:<nil> owner_name:<nil> owner_type:<nil> provision_method:build ptable_id:<nil> ptable_name:<nil> puppet_ca_proxy:map[id:1 name:foreman.l.example.org url:https://foreman.l.example.org:8443] puppet_ca_proxy_id:1 puppet_ca_proxy_name:foreman.l.example.org puppet_proxy:map[id:1 name:foreman.l.example.org url:https://foreman.l.example.org:8443] puppet_proxy_id:1 puppet_proxy_name:foreman.l.example.org puppet_status:0 pxe_loader:<nil> realm_id:<nil> realm_name:<nil> sp_ip:<nil> sp_mac:<nil> sp_name:<nil> sp_subnet_id:<nil> subnet6_id:<nil> subnet6_name:<nil> subnet_id:<nil> subnet_name:<nil> updated_at:2021-05-30 20:54:23 UTC uptime_seconds:11692 use_image:<nil> uuid:<nil>]] search:<nil> sort:map[by:<nil> order:<nil>] subtotal:1 total:1]
map[page:1 per_page:20 results:[map[architecture_id:1 architecture_name:x86_64 build:false capabilities:[build] certname:foreman.l.example.org comment:<nil> compute_profile_id:<nil> compute_profile_name:<nil> compute_resource_id:<nil> compute_resource_name:<nil> configuration_status:0 configuration_status_label:Out of sync created_at:2021-05-30 20:54:20 UTC disk:<nil> domain_id:1 domain_name:l.example.org enabled:true environment_id:1 environment_name:production global_status:0 global_status_label:Warning hostgroup_id:<nil> hostgroup_name:<nil> hostgroup_title:<nil> id:1 image_file: image_id:<nil> image_name:<nil> installed_at:<nil> ip:<nil> ip6:2001:470:7b79:100::44e last_compile:<nil> last_report:2021-05-30 20:54:15 UTC location_id:2 location_name:Default Location mac:ba:a6:69:6e:31:6c managed:false medium_id:<nil> medium_name:<nil> model_id:1 model_name:Latitude E7250 name:foreman.l.example.org operatingsystem_id:1 operatingsystem_name:CentOS 7.9.2009 organization_id:1 organization_name:Default Organization owner_id:<nil> owner_name:<nil> owner_type:<nil> provision_method:build ptable_id:<nil> ptable_name:<nil> puppet_ca_proxy:map[id:1 name:foreman.l.example.org url:https://foreman.l.example.org:8443] puppet_ca_proxy_id:1 puppet_ca_proxy_name:foreman.l.example.org puppet_proxy:map[id:1 name:foreman.l.example.org url:https://foreman.l.example.org:8443] puppet_proxy_id:1 puppet_proxy_name:foreman.l.example.org puppet_status:0 pxe_loader:<nil> realm_id:<nil> realm_name:<nil> sp_ip:<nil> sp_mac:<nil> sp_name:<nil> sp_subnet_id:<nil> subnet6_id:<nil> subnet6_name:<nil> subnet_id:<nil> subnet_name:<nil> updated_at:2021-05-30 20:54:23 UTC uptime_seconds:11692 use_image:<nil> uuid:<nil>]] search:<nil> sort:map[by:<nil> order:<nil>] subtotal:1 total:1]



I understand this map[string]interface{} allows me to not have to define every json struct beforehand, but how can I parse it?

Thanks in advance.
Regards,
Natxo

natxo....@gmail.com

unread,
May 31, 2021, 12:45:36 AM5/31/21
to golang-nuts
hi,

I got privately an answer to get the type of variable, so I tried this:

client := foreman.Client("foreman.l.example.org", "admin", "whatever", false, "")
      resp, err := client.Get("hosts")
      if err != nil {
          fmt.Println(err)
      }
 
      fmt.Printf("%T\n", resp)

and the result is:
map[string]interface {}

so this is my problem, I don't know what for type in resp is :?.

I'll take a look a the external library..

Thanks!

natxo....@gmail.com

unread,
May 31, 2021, 4:08:32 AM5/31/21
to golang-nuts
ok, I think I understand how this library works, and posting this for future reference in case someone needs it.

The library uses this other library: https://github.com/mitchellh/mapstructure

So now I can do this and vim go will helpfully fill in the struct fields for me.

client := foreman.Client("foreman.l.example.org", "admin", "whatever", false, "")

resp, err := client.Get("hosts")
if err != nil {
    fmt.Println(err)
 }

var host foreman.Host
err = mapstructure.Decode(resp, &host)

if err != nil {
    fmt.Println(err)
 }
 fmt.Println(host.Capabilities...)
                      Capabilities                      m []interface{}
 }                    Architecture_id                   m int
~                        Build                             m bool
~                        Comment                           m string
~                        Compute_attributes                m foreman.Compute_attributes
~                        Compute_profile_id                m int
~                        Compute_resource_id               m int
~                        Domain_id                         m int
~                        Enabled                           m bool
~                        Environment_id                    m int
~                        Host_parameters_attributes_map    m map[string]foreman.Params_archetype
~                        Hostgroup_id                      m int
~                        Image_id                          m int
~                        Interfaces_attributes_array       m []foreman.Interfaces_attributes
~                        Ip                                m string
~                        Location_id                       m int
~                        Mac                               m string
~                        Managed                           m bool
~                        Medium_id                         m int
~                        Model_id                          m int
~                        Name                              m string
~                        Operatingsystem_id                m int
~                        Organization_id                   m int
~                        Owner_id                          m int
~                        Owner_type                        m string
~                        Progress_report_id                m string
~                        Provision_method                  m string
~                        Ptable_id                         m int
~                        Puppet_ca_proxy_id                m int
~                        Puppet_proxy_id                   m int
~                        Puppetclass_ids                   m []int
~                        Realm_id                          m int
~                        Root_pass                         m string
~                        Subnet_id                         m int
~                        Compute_attributes.Corespersocket m string
~                        Compute_attributes.Cpus           m string
~                        Compute_attributes.Start          m string

Thanks for your pointer.

Regards,
Natxo

Amit Saha

unread,
May 31, 2021, 4:37:32 AM5/31/21
to natxo....@gmail.com, golang-nuts
In case you think this is a bit overkill for you and you may choose to do something “simpler", you can call the foreman API yourself and then you could use something like https://mholt.github.io/json-to-go/ to parse the JSON yourself in your program.


Regards,
Natxo
Foreman GET: map[page:1 per_page:20 results:[map[architecture_id:1 architecture_name:x86_64 build:false capabilities:[build] certname:foreman.l.example.org comment:<nil> compute_profile_id:<nil> compute_profile_name:<nil> compute_resource_id:<nil> compute_resource_name:<nil> configuration_status:0 configuration_status_label:Out of sync created_at:2021-05-30 20:54:20 UTC disk:<nil> domain_id:1 domain_name:l.example.org enabled:true environment_id:1 environment_name:production global_status:0 global_status_label:Warning hostgroup_id:<nil> hostgroup_name:<nil> hostgroup_title:<nil> id:1 image_file: image_id:<nil> image_name:<nil> installed_at:<nil> ip:<nil> ip6:2001:470:7b79:100::44e last_compile:<nil> last_report:2021-05-30 20:54:15 UTC location_id:2 location_name:Default Location mac:ba:a6:69:6e:31:6c managed:false medium_id:<nil> medium_name:<nil> model_id:1 model_name:Latitude E7250 name:foreman.l.example.org operatingsystem_id:1 operatingsystem_name:CentOS 7.9.2009 organization_id:1 organization_name:Default Organization owner_id:<nil> owner_name:<nil> owner_type:<nil> provision_method:build ptable_id:<nil> ptable_name:<nil> puppet_ca_proxy:map[id:1 name:foreman.l.example.org url:https://foreman.l.example.org:8443] puppet_ca_proxy_id:1 puppet_ca_proxy_name:foreman.l.example.org puppet_proxy:map[id:1 name:foreman.l.example.orgurl:https://foreman.l.example.org:8443] puppet_proxy_id:1 puppet_proxy_name:foreman.l.example.orgpuppet_status:0 pxe_loader:<nil> realm_id:<nil> realm_name:<nil> sp_ip:<nil> sp_mac:<nil> sp_name:<nil> sp_subnet_id:<nil> subnet6_id:<nil> subnet6_name:<nil> subnet_id:<nil> subnet_name:<nil> updated_at:2021-05-30 20:54:23 UTC uptime_seconds:11692 use_image:<nil> uuid:<nil>]] search:<nil> sort:map[by:<nil> order:<nil>] subtotal:1 total:1]
map[page:1 per_page:20 results:[map[architecture_id:1 architecture_name:x86_64 build:false capabilities:[build] certname:foreman.l.example.org comment:<nil> compute_profile_id:<nil> compute_profile_name:<nil> compute_resource_id:<nil> compute_resource_name:<nil> configuration_status:0 configuration_status_label:Out of sync created_at:2021-05-30 20:54:20 UTC disk:<nil> domain_id:1 domain_name:l.example.org enabled:true environment_id:1 environment_name:production global_status:0 global_status_label:Warning hostgroup_id:<nil> hostgroup_name:<nil> hostgroup_title:<nil> id:1 image_file: image_id:<nil> image_name:<nil> installed_at:<nil> ip:<nil> ip6:2001:470:7b79:100::44e last_compile:<nil> last_report:2021-05-30 20:54:15 UTC location_id:2 location_name:Default Location mac:ba:a6:69:6e:31:6c managed:false medium_id:<nil> medium_name:<nil> model_id:1 model_name:Latitude E7250 name:foreman.l.example.org operatingsystem_id:1 operatingsystem_name:CentOS 7.9.2009 organization_id:1 organization_name:Default Organization owner_id:<nil> owner_name:<nil> owner_type:<nil> provision_method:build ptable_id:<nil> ptable_name:<nil> puppet_ca_proxy:map[id:1 name:foreman.l.example.org url:https://foreman.l.example.org:8443] puppet_ca_proxy_id:1 puppet_ca_proxy_name:foreman.l.example.org puppet_proxy:map[id:1 name:foreman.l.example.orgurl:https://foreman.l.example.org:8443] puppet_proxy_id:1 puppet_proxy_name:foreman.l.example.orgpuppet_status:0 pxe_loader:<nil> realm_id:<nil> realm_name:<nil> sp_ip:<nil> sp_mac:<nil> sp_name:<nil> sp_subnet_id:<nil> subnet6_id:<nil> subnet6_name:<nil> subnet_id:<nil> subnet_name:<nil> updated_at:2021-05-30 20:54:23 UTC uptime_seconds:11692 use_image:<nil> uuid:<nil>]] search:<nil> sort:map[by:<nil> order:<nil>] subtotal:1 total:1]



I understand this map[string]interface{} allows me to not have to define every json struct beforehand, but how can I parse it?

Thanks in advance.
Regards,
Natxo

-- 
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/59b83f8e-555e-4924-a3f2-84105083253dn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages