scott
scott

Reputation: 1637

Trouble unmarshalling nested json with unknown keys

I am having trouble unmarshalling a json data of the below format to a struct. The structure of the json looks a bit confusing to me, so apologies for all the dumb things I am doing to unmarshal it.

{
  "message": {
    "Server1.example.com": [
      {
        "application": "Apache", 
        "host": {
          "name": "/^Server-[13456]/"
        }, 
        "owner": "User1", 
        "project": "Web", 
        "subowner": "User2"
      }
    ], 
    "Server2.example.com": [
      {
        "application": "Mysql", 
        "host": {
          "name": "/^Server[23456]/"
        }, 
        "owner": "User2", 
        "project": "DB", 
        "subowner": "User3"
      }
    ] 
  }, 
  "response_ms": 659, 
  "success": true
}

I am trying to unmarshal it using the following struct.

type ServerDetails struct  {
  Message  struct{
    Hostname struct{
      Details struct{
        Application string `json:"application"`
        }`json:"-"`
       }`json:"-"`
     }`json:"message"`
}

The fields Server[0-9].example.com will be unknown at the time of generating, and will change, and there is this field

      {
    "application": "Apache", 
    "host": {
      "name": "/^Server-[13456]/"
    },

just after the server name that doesn't have a key outside, which again looks confusing to me. I tried a good number of combinations to understand how this could be unmarshalled, but I failed.

What is a working approach to get the json fields unmarshal into a struct?

Upvotes: 0

Views: 1434

Answers (2)

user2326871
user2326871

Reputation: 239

You could include a map[string]ServerStruct to fullfill your requirements.

your struct could look like this:

type YourStruct struct {
    Success bool
    ResponseMS int
    Servers map[string]*ServerStruct
}

type ServerStruct struct {
    Application string
    Owner string
    [...]
}

With some additional json tags, you will be able to parse your json.

Upvotes: 3

Franck Jeannin
Franck Jeannin

Reputation: 6844

You JSON is not valid with superfluous comma after the second ] Once you correct the JSON, you can use the excellent https://mholt.github.io/json-to-go/ to build the following Go struct

type AutoGenerated struct {
    Message struct {
        Server1ExampleCom []struct {
            Application string `json:"application"`
            Host struct {
                Name string `json:"name"`
            } `json:"host"`
            Owner string `json:"owner"`
            Project string `json:"project"`
            Subowner string `json:"subowner"`
        } `json:"Server1.example.com"`
        Server2ExampleCom []struct {
            Application string `json:"application"`
            Host struct {
                Name string `json:"name"`
            } `json:"host"`
            Owner string `json:"owner"`
            Project string `json:"project"`
            Subowner string `json:"subowner"`
        } `json:"Server2.example.com"`
    } `json:"message"`
    ResponseMs int `json:"response_ms"`
    Success bool `json:"success"`
}

Upvotes: 0

Related Questions