Home > database >  Parse json array into list of struct golang
Parse json array into list of struct golang

Time:12-29

I have a json like below and I am trying to make a struct for below json which can store the data for me when I unmarshall it.

{
  "clientMetrics": [
    {
      "clientId": 951231,
      "customerData": {
        "Process": [
          "ABC"
        ],
        "Mat": [
          "KKK"
        ]
      },
      "legCustomer": [
        8773
      ]
    },
    {
      "clientId": 1234,
      "legCustomer": [
        8789
      ]
    },
    {
      "clientId": 3435,
      "otherIds": [
        4,
        32,
        19
      ],
      "legCustomer": [
        10005
      ]
    },
    {
      "clientId": 9981,
      "catId": 8,
      "legCustomer": [
        13769
      ]
    },
    {
      "clientId": 12124,
      "otherIds": [
        33,
        29
      ],
      "legCustomer": [
        12815
      ]
    },
    {
      "clientId": 8712,
      "customerData": {
        "Process": [
          "College"
        ]
      },
      "legCustomer": [
        951
      ]
    },
    {
      "clientId": 23214,
      "legCustomer": [
        12724,
        12727
      ]
    },
    {
      "clientId": 119812,
      "catId": 8,
      "legCustomer": [
        14519
      ]
    },
    {
      "clientId": 22315,
      "otherIds": [
        32
      ],
      "legCustomer": [
        12725,
        13993
      ]
    },
    {
      "clientId": 765121,
      "catId": 8,
      "legCustomer": [
        14523
      ]
    }
  ]
}

clientMetrics is a json array which contains each clientMetric object. Each clientMetric object can have various fields into it. I tried something like below but I am confuse on how to add rest since I am coming from Java background and I don't see there is set available in golang. Also confuse on how to add customerData object too.

type ClientMetrics struct {
    ClientId    int64
    CatId       int64

  

}

What is the best way to unmarshall above json into a list of ClientMetrics struct in golang?

CodePudding user response:

You can use json to go here : https://mholt.github.io/json-to-go/

But it will repeat CustomerData struct twice make sure you should remove one of them.

I have created a sample struct for your scenario as follows :

type AutoGenerated struct {
        ClientMetrics []struct {
            ClientID     int `json:"clientId"`
            CustomerData struct {
                Process []string `json:"Process"`
                Mat     []string `json:"Mat"`
            } `json:"customerData,omitempty"`
            LegCustomer []int `json:"legCustomer"`
            OtherIds    []int `json:"otherIds,omitempty"`
            CatID       int   `json:"catId,omitempty"`
        } `json:"clientMetrics"`
    }

You can run it here in go playground : https://go.dev/play/p/R1M1HfzpEny

CodePudding user response:

If you're using VS Code, there are a few extensions that can do this job. One of them is named Paste JSON as Code.

  1. Install the extension
  2. Copy the JSON and in your clipboard (ctrl c)
  3. Press Ctrl Shift P and select Paste JSON as code
  4. Type name of struct and press enter

If this doesn't work for you, you can always use this site https://mholt.github.io/json-to-go/ but it would be a better practice to use the struct obtained after unselecting Inline type definitions option.

  • Related