'How to parse same filed with different formats in Go json.Unmarshal? [duplicate]

How to parse old json and new json simultaneously with the same struct and filed?

I am new to Go, is there any easier way to solve it? Thanks so much for any advice.

I would like to parse Json from MQTT, and I can parsed below old json successfully with below Go code, now we have new json with different formats only with test filed, and I will got the error of json: cannot unmarshal array into Go struct field Skill.SKILL.Test of type float64 when parse new json.

Old json
{
  "ID" : "1",
  "SKILL" : {
    "name" : "test_name",
    // index is the `ID` of value, the length of the list is settled
    "test" : [23.1234, 15.2345, 25.2341, 27.1234, 14.2345, 16.2341,17.2341] 
  }
}
Go code

package main

import (
    "encoding/json"
)

type MyProject struct {
    Id   string `json:"Id"`
    Skill Skill `json:"SKILL"`
}

type Skill struct {
    Name                string           `json:"Name"`
    Test                []float64        `json:"Test"`
}

func main(client mqtt.Client, msg mqtt.Message) {
    myproject := MyProject{}
    err := json.Unmarshal(msg.Payload(), &myproject) 
    test := myproject.Skill.Test
}

New json

Now we have new json as below from MQTT server:

{
  "ID" : "1",
  "SKILL" : {
    "name" : "test_name",
    // `1`,`2`,`3` is the `ID` of value,the length of the list is settled
    "test" : [[1, 23.1234], [2, 13.2345], [3, 26.2341]]
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source