使用给定数据创建一个JSON数据作为map [string] interface

Below is the data That is receive from the

{
  "details": [
    {
      "attdetails": [
        {
          "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",
          "name": "compute01"
        },
        {
          "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",
          "name": "compute02"

        }
      ],
      "name": "item1"
    },
    {
      "attdetails": [
        {
          "id": "85bdafa7-274e-4180-b76f-12f390a274fc",
          "name": "compute03"
        },
        {
          "id": "85bdafa7-274e-4180-b76f-12f390a274fc",
          "name": "compute04"
        }
      ],
      "name": "item1"
    }
  ]
}

I am trying to create a JSON with the below data:

["item1":
   {"compute01": "a48c8539-caaf-49a5-9346-8e88e60e7af4"},
   {"compute02": "a48c8539-caaf-49a5-9346-8e88e60e7af4"}, 
   {"compute03": "a48c8539-caaf-49a5-9346-8e88e60e7af4"}, 
   {"compute04": "a48c8539-caaf-49a5-9346-8e88e60e7af4"}
]

I tries to create map[string]interface but I am not able to group them together and add them in an array to a particular key.

Below is the sample code I am trying with:

var childrens map[string]interface{}

for _, a := range atts {
    att, ok := childrens[a.Name]
    if !ok {
        childrens[a.Name] = make([]map[string]string, 0)
    }
    var c map[string]string
    for _, each := range a.Details {
        c[each.Name] = each.Value
    }

    childrens[a.Name] = append(c, childrens[a.Name])
}

I think that something like below should do the trick,

https://play.golang.org/p/YbZ1niXyFBR

It is not exactly the structures that you wanted but should give you the point in the direction to adjust.

I was able to get the required structure using the below code:

var cs = make(map[string][]map[string]string)


for _, a := range atts{
    _, ok := childrens[a.Name]
    if !ok {
        cs[a.Name] = make([]map[string]string, 0)
    }
    var c = make(map[string]string)
    for _, each := range a.Details {
        c[each.Name] = each.Value
    }
    cs[a.Name] = append(cs[a.ClassName], c)
}

Here's another option.

package main

import (
    "encoding/json"
    "fmt"
)

var raw = `
{
"details": [
    {
    "attdetails": [
        {
        "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",
        "name": "compute01"
        },
        {
        "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",
        "name": "compute02"

        }
    ],
    "name": "item1"
    },
    {
    "attdetails": [
        {
        "id": "85bdafa7-274e-4180-b76f-12f390a274fc",
        "name": "compute03"
        },
        {
        "id": "85bdafa7-274e-4180-b76f-12f390a274fc",
        "name": "compute04"
        }
    ],
    "name": "item1"
    }
]
}
`

type Data struct {
    Details []struct {
        AttDetails []struct {
            Id   string `json:"id"`
            Name string `json:"name"`
        } `json:"attdetails"`
        Name string `json:"name"`
    } `json:"details"`
}

func main() {
    var data Data

    err := json.Unmarshal([]byte(raw), &data)
    if err != nil {
        fmt.Println(err)
    }

    output := map[string][]map[string]string{}
    for _, detail := range data.Details {
        for _, attdetail := range detail.AttDetails {
            output[detail.Name] = append(output[detail.Name], map[string]string{
                attdetail.Name: attdetail.Id,
            })
        }
    }

    // b, _ := json.Marshal(output) // non-pretty version
    b, _ := json.MarshalIndent(output, "", "\t")
    fmt.Println(string(b))
}