Misdan Wijaya
Misdan Wijaya

Reputation: 21

How to insert null value in JSON type in golang

i have create remote schema for some purpose i have to insert a json value like this

"convert_to_money": {"IDR":10000},

i create a mutation field like this

"convert_to_money": {
 Type: scalar.JSON,
},

resolver like this :

Convert_to_money := p.Args["convert_to_money"].(map[string]interface{})

struct like this :

type TimeOvertimePolicy struct {
    ConvertToMoney          JSONB
}

and handler like this :

func UpdateOvertimesPolicy(Convert_to_money model.JSONB) (res gqltypes.SuccessType, err error) {
  timeOvertimesPolicy := model.TimeOvertimePolicy{
        ConvertToMoney:         Convert_to_money,
    }

  err = pg.DB.Transaction(func(tx *gorm.DB) error {
    d := tx.Create(&timeOvertimesPolicy)
  }
}

How can i do input null value for case like this?

Which is running now I have to insert a variable "convert_to_money": {} like this if the variable is null

I wish i can insert variable like this if null

"convert_to_money": null

Upvotes: 2

Views: 1369

Answers (3)

Jack
Jack

Reputation: 49

If you're using gorm, you can use pgtype.JSONB to init a "null" value:

pgtype.JSONB{Status: pgtype.Null}

Upvotes: 0

M_x
M_x

Reputation: 868

You can insert vairable like this

"convert_to_money": nil

Upvotes: 0

Em Ae
Em Ae

Reputation: 8704

In go nil (null) exist for pointers. So if you want to insert a nil value then your Convert_to_money should be of a pointer type.

Upvotes: 1

Related Questions