When working with the json datatype, is there a way to ensure the input JSON must have an element?

I don't mean primary, I want the JSON that gets inserted to at least have the id, name element. It can have more but at the minimum those two must be there. Can this be done?

up vote 5 down vote accepted

You can do that with a column check:

ALTER TABLE table ADD
  CHECK ((column->'id') is not null and (column->'name') is not null);

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.