0

I have two structs named as "Invoices", "Transactions". These are GORM models. I wanna merge these structs and convert json.

Example:

type Invoice struct {
     gorm.Model
     DocType string `json:"docType"`
     Total float64 `json:"total"`
}

type Transaction struct {
     gorm.Model
     DocType string `json:"docType"`
     Total float64 `json:"total"`
     Account uint `json:"account"`
}

I wanna response like;

[
{docType:"invoice", total: "123.00"}
{docType:"transaction", account:"1", total: "124.00"}
{docType:"invoice", total: "125.00"}
]
8
  • 1
    Put them in an array and marshal: x:=[]interface{}{invoice,transaction,invoice,transaction}, json.Marshal(x) Nov 30, 2020 at 15:19
  • Is it possible ? x:=[]interface{}{[]invoice,[]transaction}, json.Marshal(x) Nov 30, 2020 at 17:29
  • That is wrong syntax. You don't have arrays within arrays. Nov 30, 2020 at 17:32
  • But these getting from database. How can I adapt your solution to GORM models ? Nov 30, 2020 at 17:37
  • I think you misunderstood what I said. If you get an invoice and a transaction from the db, you can get the JSON you want using []interface{}{inv,tr} where inv and tr are structs you retrieved from the db. Nov 30, 2020 at 17:44

1 Answer 1

0

If you want the response you list in your question you can use a generic array []interface{} and turn that into JSON.

inv1 := Invoice{
    DocType: "invoice",
    Total:   123.00,
}
inv2 := Invoice{
    DocType: "invoice",
    Total:   125.00,
}
tran := Transaction{
    DocType: "transaction",
    Total:   124.00,
    Account: 1,
}
bytes, _ := json.Marshal([]interface{}{inv1, tran, inv2})
fmt.Println(string(bytes))

It doesn't matter whether you fill the structs with values from gorm or you just initialize yourself them as I do here.


Reading the comments, it appears that you have two slices of structs and you want to merge the two into a single slice, and then encode to JSON.

You can do that like this:

arr1 := []Invoice{inv1, inv2}
arr2 := []Transaction{tran}

combined := make([]interface{}, 0, len(arr1)+len(arr2))
for i := range arr1 {
    combined = append(combined, arr1[i])
}
for i := range arr2 {
    combined = append(combined, arr2[i])
}
bytes, _ := json.Marshal(combined)
fmt.Println(string(bytes))

Here again, I'm just using slices I created myself, but these could easily have come from gorm's db.Find(&arr1).

4
  • Thanks! Its working. But, what is differences are make([]interface{}, 0, len(arr1)+len(arr2)) and var combined []interface{} ? Are there any difference ? Nov 30, 2020 at 18:39
  • The difference is one of performance: the make call creates a slice with a pre-allocated capacity which I set equal to the number of elements I expect to add. When I then add elements, it won't need to resize the underlying memory which results in faster code. This only makes a significant difference when the size of the arrays is large. Nov 30, 2020 at 18:47
  • That was a nice explanation. Thank you. Nov 30, 2020 at 18:52
  • Cheers, no worries :) Nov 30, 2020 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.