WITH list AS
(SELECT status_id, json_array_elements(lang) as row FROM status)
SELECT status_id, row -> 'val' as val FROM list
WHERE row ->> 'key' = $1
The Go code:
// query to return a list with status languages
func getlang(query string, val string) interface{} {
if len(query) > 0 {
var list []map[string]interface{}
rows, err := db.Queryx(query, val)
if err != nil {
log("no records")
}
defer rows.Close()
for rows.Next() {
row := make(map[string]interface{})
err = rows.MapScan(row)
if err != nil {
log(err.Error())
}
list = append(list, row)
}
rows.Close()
if len(list) == 0 {
return ("norec")
}
return list
}
return nil
}
The query is OK outside Go, but replaces value with garbage?
No matter if I use placeholder or not.
What am I doing wrong?
Which DB driver do you use? Does it support JSON columns natively? Why do you use a JSON column at all for just plain strings?
How do you convert the result of your DB query into the JSON you shared? How does it look like if you inspect the query result directly using fmt.Printf("%#v", list)?
Thats why I asked you which driver you use and if it supports JSON colums.
From other languages I know that there is only partial support for JSON colums, eg. Elixirs ecto does only support maps and arrays in the JSON colums, and the values have to be homogenously typed.
The latter mentions in the README that it is in maintainance mode, though the description of it reads more like a self depreciation in favour of GitHub - jackc/pgx: PostgreSQL driver and toolkit for Go, which in turn explicitly mentions JSON and JSONB support in their README.
@Sibert The driver question and answer from @niamul21 is the right answer for this, but it might miss a nice recommendation if you want to have the struct scanning from sqlx and but with the native level pgx support.
In this case, lib/pq with sqlx means you aren’t using native postgres types and also everything goes over to the db as text. The pgx library supports all the postgres types and can have better performance, gets security updates (since it isn’t archived like the repo you’re using), and finally, if you want the features of sqlx (a fine lib but it uses just the database interface and can’t use native interfaces) then you can probably move to using scany: https://github.com/georgysavva/scany