Home > OS >  How can i save a bool value as 0 or 1 to database in a json body?
How can i save a bool value as 0 or 1 to database in a json body?

Time:10-28

I have an endpoint as follows:

https://myapi/user/1234

the body is

{
  "ExpiryDays": "50",  
  "Access1": "False",
  "Access2": "True",
  "Access3": "False",
  "Address": "500",
}

In the database the columns and datatype are

 ExpiryDays  int 
 Access1     bit, not null
 Access2     bit, not null
 Access3     bit, not null
 Address     nvarchar(max)

I would have though the False would be converted to a 0 and True to 1 when inserted in the db.

In my model in MVC I have defined the fields as bool. But i get the error

The JSON value could not be converted to System.Nullable`1[System.Boolean]. Path: $.Access1 .

Any ideas as on this ?

CodePudding user response:

See this - bool should have no quotes. Ideally, it should be in lower case but I don't think it cares that much. You can also use 0 and 1 I think

Also assuming "ExpiryDays" is a number it shouldn't really be quoted either.

Try changing the body to:

{
  "ExpiryDays": 50,    
  "Access1": False,
  "Access2": True,
  "Access3": False,
  "Address": "500",
}

CodePudding user response:

try deleting the quotes for example "Access1": False,

  • Related