Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant