2022-05-28

How to enforce the shape of a JSON/JSONB in Postgres?

I am trying to store the response of questions of a survey in JSON, as they could be in boolean(Is this Bar? Yes/No), number(How much is Foo?), string(Describe what is Foo). It is working fine, but how can I enforce that for a certain question, the JSON will be of identical shape?

For example, for the question "How many Foo or Bar do you eat everyday?", I am expecting the following structure(let say it is column answer):

{
 foo: number,
 bar: number
}

How can I enforce that and keep my data consistent?



No comments:

Post a Comment