{"id":2135,"date":"2022-03-22T08:54:20","date_gmt":"2022-03-22T15:54:20","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/"},"modified":"2022-03-22T08:54:20","modified_gmt":"2022-03-22T15:54:20","slug":"validate-json-documents-in-python-using-pydantic","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/es\/validate-json-documents-in-python-using-pydantic\/","title":{"rendered":"Validate JSON Documents in Python using Pydantic"},"content":{"rendered":"\n<p><span>One of the main attractions of <\/span><b>document databases<\/b><span> is the flexibility of the document structure or schema. For the most part, the flexibility in the document schema is beneficial. However, there might be some situations where having some structure to the document might be helpful. This article shows you how to validate your JSON documents against a specified schema using the popular Python library <\/span><a href=\"https:\/\/pydantic-docs.helpmanual.io\/\"><span>pydantic<\/span><\/a><span>.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span>When do you need to validate documents?<\/span><\/h4>\n\n\n\n<p><span>A common misconception about using NoSQL databases is that no structures or document schemas are required. In most cases, applications tend to have some constraints for the data even though they may not specifically validate it. For example, there might be some fields in the document that the application depends on for functionality. An application might simply not operate correctly when some of these pydantic JSON fields are missing.<\/span><\/p>\n\n\n\n<p><span>One real-world example of this problem could be an application that reads data from another unreliable application that periodically sends bad data. It would be prudent to highlight any documents that could break the application in such cases. Developers can do this either <\/span><i><span>in the application <\/span><\/i><span>or <\/span><i><span>at the document level<\/span><\/i><span>.\u00a0<\/span><\/p>\n\n\n\n<p><span>In this approach, we specify a JSON to pydantic schema for the documents to help identify those that do not match the application specifications at the document level.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span>Generate JSON testing data<\/span><\/h4>\n\n\n\n<p><span>We use the open-source library, <\/span><a href=\"https:\/\/faker.readthedocs.io\/en\/master\/#\"><span>Faker<\/span><\/a><span>, to generate some fake user profiles for this tutorial.<\/span><\/p>\n\n\n\n<p><span>This is the structure of a single document:<\/span><\/p>\n\n\n<p>[crayon decode-attributes=&#8221;false&#8221; lang=&#8221;js&#8221; decode=&#8221;true&#8221;]{<br \/>\n  &#8220;job&#8221;: &#8220;Radiographer, diagnostic&#8221;,<br \/>\n  &#8220;company&#8221;: &#8220;Klein, Dillon and Neal&#8221;,<br \/>\n  &#8220;residence&#8221;: &#8220;2232 Jackson ForksnLake Teresa, CO 46959&#8221;,<br \/>\n  &#8220;website&#8221;: [<br \/>\n    &#8220;https:\/\/bishop-torres.net\/&#8221;<br \/>\n  ],<br \/>\n  &#8220;username&#8221;: &#8220;aabbott&#8221;,<br \/>\n  &#8220;name&#8221;: &#8220;Madison Mitchell&#8221;,<br \/>\n  &#8220;address&#8221;: &#8220;1782 Moore Hill Apt. 717nWest Stephaniestad, NM 75293&#8221;,<br \/>\n  &#8220;mail&#8221;: &#8220;amberrodriguez@hotmail.com&#8221;,<br \/>\n  &#8220;phone&#8221;: {<br \/>\n    &#8220;home&#8221;: &#8220;677-197-4239&#215;889&#8221;,<br \/>\n    &#8220;mobile&#8221;: &#8220;001-594-038-9255&#215;99138&#8221;<br \/>\n  }<br \/>\n}[\/crayon]<\/p>\n\n\n\n<p><span>To simulate the broken documents, I modify a small portion of the user profiles by deleting some of the mobile phone and mail fields. We aim to identify these records that, in the real world, would be stored in a document database like Couchbase.<\/span><\/p>\n\n\n\n<p><span>I load the generated pydantic data from JSON into a bucket on our hosted <\/span><a href=\"https:\/\/www.couchbase.com\/products\/capella\/\"><span>Couchbase Capella<\/span><\/a><span> cluster using the <\/span><i><span>import <\/span><\/i><span>functionality in the built-in web console UI for our testing. I specify the <\/span><i><span>username <\/span><\/i><span>field as the key to uniquely identify each document.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span>How to specify a schema for JSON documents?<\/span><\/h4>\n\n\n\n<p><span>In the user profile data, I expect the documents to conform to the following structure in my applications:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span>Mandatory fields:<\/span>\n<ul>\n<li><span>username<\/span><\/li>\n<li><span>name<\/span><\/li>\n<li><span>phone &#8211; with JSON elements for &#8220;home&#8221; &amp; &#8220;mobile&#8221;<\/span><\/li>\n<li><span>mail<\/span><\/li>\n<li><span>website &#8211; an array<\/span><\/li>\n<\/ul>\n<\/li>\n\n\n<li><span>Optional fields:<\/span>\n<ul>\n<li><span>company<\/span><\/li>\n<li><span>residence<\/span><\/li>\n<li><span>job<\/span><\/li>\n<li><span>address<\/span><\/li>\n<\/ul>\n<\/li>\n\n<\/ul>\n\n\n\n<p><span>Pydantic is one of the most popular libraries in Python for data validation. The syntax for specifying the schema is similar to using <\/span><a href=\"https:\/\/docs.python.org\/3\/library\/typing.html\"><span>type hints for functions in Python<\/span><\/a><span>. Developers can specify the schema by defining a model. Pydantic has a rich set of features to do a variety of JSON validations. We will walk through the representation for some user profile document specifications.<\/span><\/p>\n\n\n\n<p><span>One thing to note about pydantic is that, by default, it tries to coerce the data types by doing type conversions when possible\u2014for example, converting string &#8216;1&#8217; into a numeric 1. However, there is an option to <\/span><a href=\"https:\/\/pydantic-docs.helpmanual.io\/usage\/types\/#strict-types\"><span>enable strict type checking<\/span><\/a><span> without performing conversions.<\/span><\/p>\n\n\n\n<p><span>In this code example, you see a basic configuration for the UserProfile schema using pydantic syntax:<\/span><\/p>\n\n\n<p>[crayon decode-attributes=&#8221;false&#8221; lang=&#8221;python&#8221; decode=&#8221;true&#8221;]class UserProfile(BaseModel):<br \/>\n\u00a0\u00a0\u00a0&#8220;&#8221;&#8221;Schema for User Profiles&#8221;&#8221;&#8221;<br \/>\n\u00a0\u00a0\u00a0username: str<br \/>\n\u00a0\u00a0\u00a0name: str<br \/>\n\u00a0\u00a0\u00a0phone: Phone<br \/>\n\u00a0\u00a0\u00a0mail: str<br \/>\n\u00a0\u00a0\u00a0company: Optional[str]<br \/>\n\u00a0\u00a0\u00a0residence: Optional[str]<br \/>\n\u00a0\u00a0\u00a0website: List[HttpUrl]<br \/>\n\u00a0\u00a0\u00a0job: Optional[str]<br \/>\n\u00a0\u00a0\u00a0address: Optional[str][\/crayon]<\/p>\n\n\n\n<p><span>Each field is specified along with the expected data type. The optional fields are marked as <\/span><i><span>Optional<\/span><\/i><span>. An array is specified by the <\/span><i><span>List <\/span><\/i><span>keyword followed by the desired data type.<\/span><\/p>\n\n\n\n<p><span>The <\/span><i><span>username <\/span><\/i><span>field needs to be a string, while the <\/span><i><span>company <\/span><\/i><span>field is an optional string. If we look at the other lines in the snippet, we see the <\/span><i><span>website <\/span><\/i><span>field is a list of type <\/span><i><span>HttpUrl \u2013 <\/span><\/i><span>one of the many custom types provided by pydantic out of the box. <\/span><i><span>HttpUrl <\/span><\/i><span>is used to validate that the URL is valid and not random strings. Similarly, there are other fields like <\/span><i><span>emails <\/span><\/i><span>that we could use to ensure that the email fields are a valid form.\u00a0<\/span><\/p>\n\n\n\n<p><span>If you look at the <\/span><i><span>phone <\/span><\/i><span>field, it is marked as a <\/span><i><span>Phone <\/span><\/i><span>type which is a custom type that we will define in the next code snippet:<\/span><\/p>\n\n\n<p>[crayon decode-attributes=&#8221;false&#8221; lang=&#8221;python&#8221; decode=&#8221;true&#8221;]class Phone(BaseModel):<br \/>\n   &#8220;&#8221;&#8221;Schema for Phone numbers&#8221;&#8221;&#8221;<br \/>\n   home: str<br \/>\n   mobile: str<\/p>\n<p>   @validator(&#8220;mobile&#8221;, &#8220;home&#8221;)<br \/>\n   def does_not_contain_extension(cls, v):<br \/>\n       &#8220;&#8221;&#8221;Check if the phone numbers contain extensions&#8221;&#8221;&#8221;<br \/>\n       if &#8220;x&#8221; in v:<br \/>\n         raise ExtensionError(wrong_value=v)<br \/>\n       return v[\/crayon]<\/p>\n\n\n\n<p><span>Here we specify that the <\/span><i><span>Phone <\/span><\/i><span>is composed of two fields that are both strings: <\/span><i><span>home <\/span><\/i><span>and <\/span><i><span>mobile<\/span><\/i><span>. This would be checked inside the <\/span><i><span>UserProfile <\/span><\/i><span>model and interpreted as the <\/span><i><span>UserProfile <\/span><\/i><span>model containing a <\/span><i><span>phone <\/span><\/i><span>field that contains <\/span><i><span>home <\/span><\/i><span>and <\/span><i><span>mobile <\/span><\/i><span>fields.\u00a0<\/span><\/p>\n\n\n\n<p><span>Pydantic also allows us to validate the <\/span><i><span>contents <\/span><\/i><span>of the data and the type and presence of the data. You can do this by defining functions that validate specific fields. In the above example, we validate the <\/span><i><span>mobile <\/span><\/i><span>and <\/span><i><span>home <\/span><\/i><span>fields to check for extensions. If they contain an extension, we do not support it and throw a custom error. These schema errors are then shown to the users doing the pydantic validation.<\/span><\/p>\n\n\n\n<p><span>You can view the schema definition by specifying the <\/span><i><span>Model.schema_json()<\/span><\/i><span> method as shown here:<\/span><\/p>\n\n\n<p>[crayon decode-attributes=&#8221;false&#8221; lang=&#8221;js&#8221; decode=&#8221;true&#8221;]{<br \/>\n  &#8220;title&#8221;: &#8220;UserProfile&#8221;,<br \/>\n  &#8220;description&#8221;: &#8220;Schema for User Profiles&#8221;,<br \/>\n  &#8220;type&#8221;: &#8220;object&#8221;,<br \/>\n  &#8220;properties&#8221;: {<br \/>\n    &#8220;username&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Username&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n    },<br \/>\n    &#8220;name&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Name&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n    },<br \/>\n    &#8220;phone&#8221;: {<br \/>\n      &#8220;$ref&#8221;: &#8220;#\/definitions\/Phone&#8221;<br \/>\n    },<br \/>\n    &#8220;mail&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Mail&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n    },<br \/>\n    &#8220;company&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Company&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n    },<br \/>\n    &#8220;residence&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Residence&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n    },<br \/>\n    &#8220;website&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Website&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;array&#8221;,<br \/>\n      &#8220;items&#8221;: {<br \/>\n        &#8220;type&#8221;: &#8220;string&#8221;,<br \/>\n        &#8220;minLength&#8221;: 1,<br \/>\n        &#8220;maxLength&#8221;: 2083,<br \/>\n        &#8220;format&#8221;: &#8220;uri&#8221;<br \/>\n      }<br \/>\n    },<br \/>\n    &#8220;job&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Job&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n    },<br \/>\n    &#8220;address&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Address&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n    }<br \/>\n  },<br \/>\n  &#8220;required&#8221;: [<br \/>\n    &#8220;username&#8221;,<br \/>\n    &#8220;name&#8221;,<br \/>\n    &#8220;phone&#8221;,<br \/>\n    &#8220;mail&#8221;,<br \/>\n    &#8220;website&#8221;<br \/>\n  ],<br \/>\n  &#8220;definitions&#8221;: {<br \/>\n    &#8220;Phone&#8221;: {<br \/>\n      &#8220;title&#8221;: &#8220;Phone&#8221;,<br \/>\n      &#8220;description&#8221;: &#8220;Schema for Phone numbers&#8221;,<br \/>\n      &#8220;type&#8221;: &#8220;object&#8221;,<br \/>\n      &#8220;properties&#8221;: {<br \/>\n        &#8220;home&#8221;: {<br \/>\n          &#8220;title&#8221;: &#8220;Home&#8221;,<br \/>\n          &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n        },<br \/>\n        &#8220;mobile&#8221;: {<br \/>\n          &#8220;title&#8221;: &#8220;Mobile&#8221;,<br \/>\n          &#8220;type&#8221;: &#8220;string&#8221;<br \/>\n        }<br \/>\n      },<br \/>\n      &#8220;required&#8221;: [<br \/>\n        &#8220;home&#8221;,<br \/>\n        &#8220;mobile&#8221;<br \/>\n      ]<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n[\/crayon]<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span>JSON documents validation against a pydantic Python schema<\/span><\/h4>\n\n\n\n<p><span>Now that we have defined the schema let&#8217;s explore how we can validate the documents against the schema.<\/span><\/p>\n\n\n\n<p><span>Validation can be done by using the pydantic <\/span><i><span>parse_obj<\/span><\/i><span> method of the model. You specify the document as a dictionary and check for validation exceptions. In this case, we fetch all the documents (up to the specified limit) using a Couchbase query and test them one by one and report any errors.<\/span><\/p>\n\n\n<p>[crayon decode-attributes=&#8221;false&#8221; lang=&#8221;python&#8221; decode=&#8221;true&#8221;]password_authenticator = PasswordAuthenticator(DB_USER, DB_PWD)<br \/>\n    cluster = Cluster(<br \/>\n       CONN_STR,<br \/>\n       ClusterOptions(password_authenticator),<br \/>\n    )<br \/>\n   validation_error_count = 0<br \/>\n   query = f&#8221;select profile.* from `{BUCKET}`.{SCOPE}.{COLLECTION} profile LIMIT {DOCUMENT_LIMIT}&#8221;<br \/>\n   try:<br \/>\n      results = cluster.query(query)<br \/>\n      for row in results:<br \/>\n         try:<br \/>\n            UserProfile.parse_obj(row)<br \/>\n            validation_error_count += 1<br \/>\n         except ValidationError as e:<br \/>\n               print(f&#8221;Error found in document: {row[&#8216;username&#8217;]}n&#8221;, e.json())<br \/>\n   except Exception as e:<br \/>\n       print(e)<\/p>\n<p>   print(f&#8221;Validation Error Count: {validation_error_count}&#8221;)[\/crayon]<\/p>\n\n\n\n<p><span>The schema highlights some of the errors observed in the documents:<\/span><\/p>\n\n\n<p>[crayon decode-attributes=&#8221;false&#8221; lang=&#8221;js&#8221; decode=&#8221;true&#8221;]Error found in document: aarias<br \/>\n\u00a0[<br \/>\n\u00a0\u00a0{<br \/>\n\u00a0\u00a0\u00a0\u00a0&#8220;loc&#8221;: [<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;phone&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;home&#8221;<br \/>\n\u00a0\u00a0\u00a0\u00a0],<br \/>\n\u00a0\u00a0\u00a0\u00a0&#8220;msg&#8221;: &#8220;value is an extension, got &#8220;(932)532-4001&#215;319&#8243;&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0&#8220;type&#8221;: &#8220;value_error.extension&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0&#8220;ctx&#8221;: {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;wrong_value&#8221;: &#8220;(932)532-4001&#215;319&#8221;<br \/>\n\u00a0\u00a0\u00a0\u00a0}<br \/>\n\u00a0\u00a0},<br \/>\n\u00a0\u00a0{<br \/>\n\u00a0\u00a0\u00a0\u00a0&#8220;loc&#8221;: [<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;phone&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;mobile&#8221;<br \/>\n\u00a0\u00a0\u00a0\u00a0],<br \/>\n\u00a0\u00a0\u00a0\u00a0&#8220;msg&#8221;: &#8220;field required&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0&#8220;type&#8221;: &#8220;value_error.missing&#8221;<br \/>\n\u00a0\u00a0}<br \/>\n][\/crayon]<\/p>\n\n\n\n<p><span>The document with the ID <\/span><i><span>aarias<\/span><\/i><span> has extensions in the home field, and the <\/span><i><span>phone <\/span><\/i><span>\u2192 <\/span><i><span>mobile <\/span><\/i><span>field is also missing.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span>Summary<\/span><\/h4>\n\n\n\n<p><span>This user profile example shows how we can easily create custom schemas for our JSON documents. This post also shows how to use the test and validate capabilities of Python and the pydantic module. It is just the tip of the iceberg, though\u2013there are many more <\/span><a href=\"https:\/\/pydantic-docs.helpmanual.io\/usage\/types\/\"><span>types<\/span><\/a><span> that we can validate.\u00a0<\/span><\/p>\n\n\n\n<p><span>Other approaches are also possible; for example, you can validate data at the time of writing. This can be done quite easily by integrating the schema that we defined here with the application and verifying the data before insert\/upserting into Couchbase.<\/span><\/p>\n\n\n\n<p><span>The entire code for this demo can be found on <\/span><a href=\"https:\/\/github.com\/nithishr\/json-schema-validation\"><span>Github<\/span><\/a><span>. The instructions to generate the data and run the scripts can also be found there.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Resources<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/nithishr\/json-schema-validation\">JSON schema validation<\/a> &#8211; GitHub repository for code from this post<\/li>\n\n\n<li><a href=\"https:\/\/www.couchbase.com\/products\/capella\/\"><span>Couchbase Capella<\/span><\/a> &#8211; <a href=\"https:\/\/www.couchbase.com\/products\/capella\/\">fully hosted NoSQL DBaaS<\/a><\/li>\n\n\n<li><a href=\"https:\/\/pydantic-docs.helpmanual.io\/\">Python pydantic module<\/a><\/li>\n\n\n<li><a href=\"https:\/\/faker.readthedocs.io\/en\/master\/#\">Python Faker module<\/a><\/li>\n\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>One of the main attractions of document databases is the flexibility of the document structure or schema. For the most part, the flexibility in the document schema is beneficial. However, there might be some situations where having some structure to the document might be helpful. This article shows you how to validate your JSON documents [&hellip;]<\/p>\n","protected":false},"author":80878,"featured_media":2133,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[54,189,381],"tags":[30,538],"ppma_author":[539],"class_list":["post-2135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-couchbase-server","category-data-modeling","category-python","tag-json","tag-schema"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.6 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>JSON Validation Against Pydantic Schema Tutorial | Couchbase<\/title>\n<meta name=\"description\" content=\"Find out how to validate JSON documents against a specified schema using the popular Python library pydantic. Get validation best practices at Couchbase.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/es\/validate-json-documents-in-python-using-pydantic\/\" \/>\n<meta property=\"og:locale\" content=\"es_MX\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Validate JSON Documents in Python using Pydantic\" \/>\n<meta property=\"og:description\" content=\"Find out how to validate JSON documents against a specified schema using the popular Python library pydantic. Get validation best practices at Couchbase.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/es\/validate-json-documents-in-python-using-pydantic\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-22T15:54:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/MFA-BGimage-lg.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"nithishr\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"nithishr\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/\"},\"author\":{\"name\":\"nithishr\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/person\\\/c5a843d75ad78b5b698f59ca6d123af2\"},\"headline\":\"Validate JSON Documents in Python using Pydantic\",\"datePublished\":\"2022-03-22T15:54:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/\"},\"wordCount\":1408,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/5\\\/2026\\\/05\\\/MFA-BGimage-lg.jpg\",\"keywords\":[\"JSON\",\"Schema\"],\"articleSection\":[\"Couchbase Server\",\"Data Modeling\",\"Python\"],\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/\",\"name\":\"JSON Validation Against Pydantic Schema Tutorial | Couchbase\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/5\\\/2026\\\/05\\\/MFA-BGimage-lg.jpg\",\"datePublished\":\"2022-03-22T15:54:20+00:00\",\"description\":\"Find out how to validate JSON documents against a specified schema using the popular Python library pydantic. Get validation best practices at Couchbase.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/5\\\/2026\\\/05\\\/MFA-BGimage-lg.jpg\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/5\\\/2026\\\/05\\\/MFA-BGimage-lg.jpg\",\"width\":1200,\"height\":800},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/validate-json-documents-in-python-using-pydantic\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Validate JSON Documents in Python using Pydantic\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/5\\\/2026\\\/06\\\/logo.svg\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/wp-content\\\/uploads\\\/sites\\\/5\\\/2026\\\/06\\\/logo.svg\",\"width\":\"1024\",\"height\":\"1024\",\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/#\\\/schema\\\/person\\\/c5a843d75ad78b5b698f59ca6d123af2\",\"name\":\"nithishr\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b096d222e532a4927ddfde19dbd538f961be97604327df03ad9c3661fd703948?s=96&d=mm&r=gf71d22416bc6a658b6c66f1bb28b7934\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b096d222e532a4927ddfde19dbd538f961be97604327df03ad9c3661fd703948?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b096d222e532a4927ddfde19dbd538f961be97604327df03ad9c3661fd703948?s=96&d=mm&r=g\",\"caption\":\"nithishr\"},\"description\":\"Nithish is an engineer who loves to build products that solve real-world problems in short spans of time. He has experienced different areas of the industry having worked in diverse companies in Germany and India. Apart from work, he likes to travel and interact and engage with the tech community through Meetups &amp; Hackathons. In his free time, he likes to try stuff out by hacking things together.\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/blog\\\/es\\\/author\\\/nithishr\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"JSON Validation Against Pydantic Schema Tutorial | Couchbase","description":"Find out how to validate JSON documents against a specified schema using the popular Python library pydantic. Get validation best practices at Couchbase.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/es\/validate-json-documents-in-python-using-pydantic\/","og_locale":"es_MX","og_type":"article","og_title":"Validate JSON Documents in Python using Pydantic","og_description":"Find out how to validate JSON documents against a specified schema using the popular Python library pydantic. Get validation best practices at Couchbase.","og_url":"https:\/\/www.couchbase.com\/blog\/es\/validate-json-documents-in-python-using-pydantic\/","og_site_name":"The Couchbase Blog","article_published_time":"2022-03-22T15:54:20+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/MFA-BGimage-lg.jpg","type":"image\/jpeg"}],"author":"nithishr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"nithishr","Est. reading time":"5 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/"},"author":{"name":"nithishr","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/c5a843d75ad78b5b698f59ca6d123af2"},"headline":"Validate JSON Documents in Python using Pydantic","datePublished":"2022-03-22T15:54:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/"},"wordCount":1408,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/MFA-BGimage-lg.jpg","keywords":["JSON","Schema"],"articleSection":["Couchbase Server","Data Modeling","Python"],"inLanguage":"es","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/","url":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/","name":"JSON Validation Against Pydantic Schema Tutorial | Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/MFA-BGimage-lg.jpg","datePublished":"2022-03-22T15:54:20+00:00","description":"Find out how to validate JSON documents against a specified schema using the popular Python library pydantic. Get validation best practices at Couchbase.","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/"]}]},{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/MFA-BGimage-lg.jpg","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/MFA-BGimage-lg.jpg","width":1200,"height":800},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/validate-json-documents-in-python-using-pydantic\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Validate JSON Documents in Python using Pydantic"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/06\/logo.svg","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/06\/logo.svg","width":"1024","height":"1024","caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/c5a843d75ad78b5b698f59ca6d123af2","name":"nithishr","image":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/secure.gravatar.com\/avatar\/b096d222e532a4927ddfde19dbd538f961be97604327df03ad9c3661fd703948?s=96&d=mm&r=gf71d22416bc6a658b6c66f1bb28b7934","url":"https:\/\/secure.gravatar.com\/avatar\/b096d222e532a4927ddfde19dbd538f961be97604327df03ad9c3661fd703948?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b096d222e532a4927ddfde19dbd538f961be97604327df03ad9c3661fd703948?s=96&d=mm&r=g","caption":"nithishr"},"description":"Nithish is an engineer who loves to build products that solve real-world problems in short spans of time. He has experienced different areas of the industry having worked in diverse companies in Germany and India. Apart from work, he likes to travel and interact and engage with the tech community through Meetups &amp; Hackathons. In his free time, he likes to try stuff out by hacking things together.","url":"https:\/\/www.couchbase.com\/blog\/es\/author\/nithishr\/"}]}},"acf":[],"authors":[{"term_id":539,"user_id":80878,"is_guest":0,"slug":"nithishr","display_name":"nithishr","avatar_url":{"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/image-4-2.png","url2x":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/5\/2026\/05\/image-4-2.png"},"0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/posts\/2135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/users\/80878"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/comments?post=2135"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/posts\/2135\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/media\/2133"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/media?parent=2135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/categories?post=2135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/tags?post=2135"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/es\/wp-json\/wp\/v2\/ppma_author?post=2135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}