{"id":15,"date":"2023-11-02T22:01:05","date_gmt":"2023-11-02T22:01:05","guid":{"rendered":"https:\/\/blog.dataplatform.lt\/?p=15"},"modified":"2023-11-02T22:01:05","modified_gmt":"2023-11-02T22:01:05","slug":"nullable-and-optional-fields","status":"publish","type":"post","link":"https:\/\/blog.dataplatform.lt\/?p=15","title":{"rendered":"Nullable and optional fields"},"content":{"rendered":"\n<p>To require that some fields are always available, whitelist it to \u201crequired\u201d list.<\/p>\n\n\n\n<p class=\"has-yellow-background-color has-background\">Warning: this is different than nullable concept below. Field can be required, but nullable. In contrast, field also can be optional, but if provided it may not be null. Use example below as pattern how to handle required field when its missing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>items = requests.get(\u2018http:\/\/items.com)\nresults = &#91;]\nfor item in items:\n   result = {\n      \u2018price\u2019: item.get(\u2018kaina\u2019), # always present\n      \u2018name\u2019: item.get(\u2018pavadinimas\u2019), # always present\n   }\n\n   # schema does not allow NULL (None) value\n   discount = item.get(\u2018discount\u2019)\n   if discount is not None: \n      result&#91;\u2018discount\u2019] = discount\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Nullable fields<\/h3>\n\n\n\n<p>To allow \u201cNone\u201d value for field, you must make it nullable.&nbsp;<\/p>\n\n\n\n<p>Use case example: in the apartments list table, sold apartments usually has no longer pricing information. For apartments which are available to purchase this data is still available.<\/p>\n\n\n\n<p>Currently the approach to make nullable is to use text version of JSON schema editor, and change type of field, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \"price\": {\n       \"type\": &#91;\n         \"number\",\n         \"null\"\n       ]\n     },<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To require that some fields are always available, whitelist it to \u201crequired\u201d list. Warning: this is different than nullable concept below. Field can be required, but nullable. In contrast, field also can be optional, but if provided it may not be null. Use example below as pattern how to handle required field when its missing. &hellip; <a href=\"https:\/\/blog.dataplatform.lt\/?p=15\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Nullable and optional fields&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-format"],"_links":{"self":[{"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":1,"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":16,"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions\/16"}],"wp:attachment":[{"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.dataplatform.lt\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}