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

Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
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.success_dt as dt,, p.payment_sum, p.pay_system_id, 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 = 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}]"
"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 | 32 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(, &result)
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}]",
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.

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
38,662 users