# Issues retrieving json from postgresql

**URL:** <https://forum.golangbridge.org/t/issues-retrieving-json-from-postgresql/28712>\
**Category:** Getting Help\
**Created:** [September 3, 2022, 3:42pm UTC](https://forum.golangbridge.org/t/issues-retrieving-json-from-postgresql/28712 "2022-09-03T15:42:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rexlx](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/rexlx/32/11261_2.png) [@rexlx](https://forum.golangbridge.org/u/rexlx)\
**Post date:** [September 3, 2022, 3:42pm UTC](https://forum.golangbridge.org/t/issues-retrieving-json-from-postgresql/28712/1 "2022-09-03T15:42:43Z")

</div>

hey everyone 🙂  
im making an api for funsies and ran into a problem when trying to get a test entry from the database. maybe I datafilled it wrong, or maybe im doing everything wrong…idk.

here is what im trying to retrieve:

```auto
type Config struct {
	ID int `json:"id"`
	Name string `json:"name"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Plan Plan `json:"plan"`
}

type Plan struct {
	Name string `json:"name"`
	Steps []Instruction `json:"steps"`
}

```

the error I get from go is: Scan error on column index 3, name “plan”: pq: destination \*data.Plan is not a pointer to array or slice

here is the query:

```auto
q := `select id, created_at, updated_at, plan from configurations`
	rows, err := db.QueryContext(ctx, q)
	if err != nil {
		return nil, err
	}

	defer rows.Close()
	var configs []*Config

	for rows.Next() {
		var cfg Config
		err := rows.Scan(
			&cfg.ID,
			&cfg.CreatedAt,
			&cfg.UpdatedAt,
			// &cfg.Plan,
			pq.Array(&cfg.Plan),
		)
		if err != nil {
			return nil, err
		}
		configs = append(configs, &cfg)
	}

```

here is the json i tried storing in the database as JSONB:

```auto
1 {                                                                                               
  2 "name": "jabber",                                                                           
  3 "steps": [{                                                                                 
  4 "id": 1,                                                                                
  5 "name": "first",                                                                        
  6 "hosts": ["fake1", "fake2"],                                                            
  7 "command": "putup",                                                                     
  8 "fatal": false,                                                                         
  9 "timeout": 62                                                                           
 10 }]                                                                                          
 11                                                                                                 
 12 }

```

thanks much!

---

<div class="post-metadata">

**Author:** ![christophberger](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/christophberger/32/11391_2.png) [@christophberger](https://forum.golangbridge.org/u/christophberger)\
**Post date:** [September 3, 2022, 4:01pm UTC](https://forum.golangbridge.org/t/issues-retrieving-json-from-postgresql/28712/2 "2022-09-03T16:01:36Z")

</div>

Hi @rexlx,

The DB column `plan` appears to contain the JSON string you posted, correct? The code tries to scan this JSON string into a field of type `Plan` (a struct type).

What I would try is to scan the `plan` column into a string and then unmarshal that string into the `Plan` and `Instructions` structs (using `encoding/json`).

---

<div class="post-metadata">

**Author:** ![rexlx](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/rexlx/32/11261_2.png) [@rexlx](https://forum.golangbridge.org/u/rexlx)\
**Post date:** [September 3, 2022, 4:22pm UTC](https://forum.golangbridge.org/t/issues-retrieving-json-from-postgresql/28712/3 "2022-09-03T16:22:03Z")

</div>

I think this will do the trick, I appreciate it @christophberger

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [December 2, 2022, 4:22pm UTC](https://forum.golangbridge.org/t/issues-retrieving-json-from-postgresql/28712/4 "2022-12-02T16:22:59Z")

</div>

Closed on December 2, 2022, 4:22pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
