Structure with a field containing a JSON string. As of this structure to get the correct JSON?

0 like 0 dislike
6 views
Good day!

there is a structure
type DetailedReportRow struct { Id int64 `json:"id"` Dt *time.Time `json:"dt"` Phone string `json:"phone"` TicketSums string `json:"tickets" sql:"type:json"` PaymentSum float32 `json:"payment_sum"` PaySystemId float32 `json:"pay_system_id"` PayStateName string `json:"pay_state_name"` BusNo string `json:"bus_no"` }


there is a request to the database PostgreSQL, the result of which populates an array of instances of this structure
... var result []models.DetailedReportRow ... slq := fmt.Sprintf(` select p.id p.success_dt as dt, p.phone, p.payment_sum, p.pay_system_id, ps.name as pay_state_name, bb.state_number as bus_no, p.json_data_r::json->>'s' as ticket_sums is the element s contains an array of simple objects of the form [{"p": 90}, {"p": 90}] FROM pay_payment p Pay_state LEFT JOIN ps on ps.id = p.pay_state_id Bus_bus LEFT JOIN bb on (bb.qr = p.json_data_r::json->>'q') ... `) controllers.DB.Raw(slq).Scan(&result) return result

field ticket_sums, of course, returned as a string.
next, the result vozvrashaetsja the client from the controller Revel:
func (c ManagerAuthApiCtl) BusDetailedReport() revel.Result { ... stat := repository.Bus().DetailedReport(date, system, fleet) return c.RenderJSON(JsonResponse{ Success: true, Data: map[string]interface{}{"stat": stat}, }) }


the client arrives json like this:
{ "success": true "data": { "stat": [ { "id": 185247, "dt": "2018-10-18T18:36:07Z", "phone": "0000000000", "tickets": "[{\\"p\\": 90}, {\\"p\\": 90}]", "payment_sum": 180, "pay_system_id": 1, "pay_state_name": "Successfully paid", "bus_no": "157 at 01" }, ... ] } }

you need to
"tickets": "[{\\"p\\": 90}, {\\"p\\": 90}]"
was:
"tickets": [{"p": 90}, {"p": 90}]
i.e. as an array of objects, not a string with escaped quotes

search on the Internet has led to the fact that the field structure containing json should be declared like this:
type DetailedReportRaw struct { ... TicketSums json.RawMessage `json:"tickets"` ... }

but in the issued json'e field tickets is equal to nil

Prompt, who faced how to get the desired result?
Thank you!
by | 6 views

3 Answers

0 like 0 dislike
You just need to make the tickets a field of type string and unmarshaling right out of him, leading to type []byte.

result := map[string]int{} json.Unmarshal([]byte(someStruct.tickets), &result)
by
0 like 0 dislike
It is necessary for the client to throw the slashes from the string and all - str.replace(/\\\\/g, ");
"tickets": "[{"p": 90}, {"p": 90}]",
by
0 like 0 dislike
search on the Internet led to json.RawMessage

the direction is correct
but at the same time in issued json'e field tickets is equal to nil

it is possible that controllers.DB.Raw(slq).Scan(&result) not can portray make in qualify to work with this type
try what happens if you declare the field as
TicketSums []byte
whether it data, if Yes, then you can dig further

to unmarshaling line, as suggested above, it is possible, if you need to quickly fix. As a solution - spike, the unnecessary overhead for decoding/encoding a ready json.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Jun 10, 2019 by starosta46
0 like 0 dislike
3 answers
asked Apr 9, 2019 by Pavel_Timofeev
110,608 questions
257,186 answers
0 comments
28,740 users