https://play.golang.org/p/548a20mPSoR
import (
"encoding/json"
"fmt"
"log"
"os/exec"
"strings"
)
type Company struct {
ID struct {
Oid string `json:"$oid"`
} `json:"_id"`
Name string `json:"name"`
Pin string `json:"Pin"`
}
func (c Company) MarshalJSON() ([]byte, error) {
uuid, _ := exec.Command("uuidgen").Output()
uuid0 := strings.ReplaceAll(string(uuid),"\n","")
le := len(strings.Split(c.Name, " "))
conc := c.Name[:5] + " " + c.Pin[:5]
n := struct {
Name string `json:"name"`
Pin string `json:"pin"`
Pid string `json:"pid"`
Words int `josn:"words"`
Dummy string `json:"dummy"`
}{
Name: c.Name,
Pin: c.Pin,
Pid: uuid0,
Words: le,
Dummy: conc,
}
ans, error:= json.Marshal(n)
if error !=nil {
log.Fatal(error)
}
return ans, error
}
func main() {
data := []byte(`[{"_id": {"$oid": "585cbb871"}, "name": "Pqr Mn", "pin": "U2334"}, {"_id": {"$oid": "305a6776912"}, "name": "Abc Xyz", "pin": "U12345"}, {"_id": {"$oid": "5a704ed1b"}, "name": "Lbs Ll", "pin": "U34567"}]`)
c := []Company{}
error := json.Unmarshal(data,&c)
if error != nil{
log.Fatal(error)
}
out, _ := json.Marshal(c)
fmt.Println(string(out))
}```