Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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 ?

>Solution :

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",
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading