Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Unable to decode single-dimension json array #308

Open
AlastairDewar opened this issue Oct 7, 2022 · 0 comments
Open

Unable to decode single-dimension json array #308

AlastairDewar opened this issue Oct 7, 2022 · 0 comments

Comments

@AlastairDewar
Copy link

Hello,

I've come across and issue where when trying to convert a Gorm Model, which has a field of gorm.io/datatypes/JSON, and a value similar to ["a", "b", "c"].

It looks as though the assignment of the fieldType variable on line 1310 of mapstructure.go using reflection is incorrectly returning an integer type, and not a string. Given the ability to have json arrays with [1, "2"] - it might be better just to allow for a an interface{} when looking at arrays/json?

The resultant output is

2022/10/07 10:49:49 Unable to decode MyObject object: 5 error(s) decoding:

* 'my_field[0]' expected type 'uint8', got unconvertible type 'string', value: 'a'
* 'my_field[1]' expected type 'uint8', got unconvertible type 'string', value: 'b'
* 'my_field[2]' expected type 'uint8', got unconvertible type 'string', value: 'c'

Example Gorm Model

type Video struct {
	ID             uint           `gorm:"primaryKey; type:bigint; not null;" json:"id"`
	MyField   datatypes.JSON `gorm:"type:jsonb;default:'[]';" json:"my_field" mapstructure:"my_field"`
	CreatedAt      time.Time      `json:"created_at"`
	UpdatedAt      time.Time      `json:"updated_at"`
}

All the best,
Ally

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant