{"id":27408,"date":"2017-03-11T11:55:22","date_gmt":"2017-03-11T09:55:22","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=27408"},"modified":"2017-03-16T09:32:15","modified_gmt":"2017-03-16T07:32:15","slug":"validating-json-against-schema-in-php","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/","title":{"rendered":"Validating JSON against schema in PHP"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). \u00a0Staring at a slowly scrolling list of installed dependencies, I noticed something interesting.<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n...\r\n  - Installing seld\/jsonlint (1.6.0)\r\n  - Installing justinrainbow\/json-schema (5.1.0)\r\n...\r\n<\/pre>\n<p>Of course, I&#8217;ve heard of the <a href=\"https:\/\/github.com\/Seldaek\/jsonlint\">seld\/jsonlint<\/a> before. \u00a0It&#8217;s a port of <a href=\"https:\/\/github.com\/zaach\/jsonlint\">zaach\/jsonlint<\/a> JavaScript tool to PHP, written by\u00a0<a href=\"https:\/\/github.com\/Seldaek\">Jordi Boggiano<\/a>, aka Seldaek, the genius who brought us <a href=\"https:\/\/getcomposer.org\">composer dependency manager<\/a> and <a href=\"https:\/\/packagist.org\/\">packagist.org<\/a> repository.<\/p>\n<p>But JSON schema? What&#8217;s that?<\/p>\n<p>The last time I heard the word &#8220;schema&#8221; in a non-database context, it was in the XML domain. \u00a0And I hate XML with passion. \u00a0It&#8217;s ugly and horrible and should die a quick death. \u00a0The sooner, the better.<\/p>\n<p>But with all its ugliness, XML has does something right &#8211; it allows the schema definition, against which the XML file can be validated later.<\/p>\n<p>Can I have the same with JSON? \u00a0Well, apparently, yes!<\/p>\n<p><a href=\"https:\/\/github.com\/justinrainbow\/json-schema\">justinrainbow\/json-schema<\/a> package allows one to define a schema for what&#8217;s allowed in the JSON file, and than validate against it. \u00a0And even more than that &#8211; it supports both required values and default values too.<\/p>\n<p>Seeing the package being installed right next to something by Seldaek, I figured, composer might be using it already. \u00a0A quick look <a href=\"https:\/\/github.com\/composer\/composer\/tree\/master\/res\">in the repository<\/a> confirmed my suspicion. \u00a0<a href=\"https:\/\/getcomposer.org\/doc\/04-schema.md#json-schema\">Composer documentation<\/a> provides more information, and links to an even more helpful <a href=\"http:\/\/json-schema.org\/\">JSON-Schema.org<\/a>.<\/p>\n<p>Mind. \u00a0Officially. \u00a0Blown.<\/p>\n<p>At work, we use a whole lot of configuration files for many of our projects. \u00a0Those files which are intended for tech-savvy users, are usually in JSON or PHP format, without much validation attached to them. \u00a0 Those files which are for non-technical users, usually rely on even simpler formats like INI and CSV. \u00a0I see this all changing and improving soon.<\/p>\n<p>But before any of that happens, I need to play around with these amazing tools. \u00a0Here&#8217;s a quick first look that I did:<\/p>\n<ol>\n<li>Install the JSON validator:\u00a0<em>composer require justinrainbow\/json-schema<\/em><\/li>\n<li>Create an example <em>config.json<\/em> file that I will be validating.<\/li>\n<li>Create a simple <em>schema.json<\/em> file that defines what is valid.<\/li>\n<li>Create a simple i<em>ndex.php<\/em> file to tie it altogether, mostly just coping code from the documentation.<\/li>\n<\/ol>\n<p>My <em>config.json<\/em> file looks like this:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n\t&quot;blah&quot;: &quot;foobar&quot;,\r\n\t&quot;foo&quot;: &quot;bar&quot;\r\n}\r\n<\/pre>\n<p>My <em>schema.json<\/em> file looks like this:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n\t&quot;type&quot;: &quot;object&quot;,\r\n\t&quot;properties&quot;: {\r\n\t\t&quot;blah&quot;: {\r\n\t\t\t&quot;type&quot;: &quot;string&quot;\r\n\t\t},\r\n\t\t&quot;version&quot;: {\r\n\t\t\t&quot;type&quot;: &quot;string&quot;,\r\n\t\t\t&quot;default&quot;: &quot;v1.0.0&quot;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>And, finally, my <em>index.php<\/em> file looks like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nrequire_once 'vendor\/autoload.php';\r\n\r\nuse JsonSchema\\Validator;\r\nuse JsonSchema\\Constraints\\Constraint;\r\n\r\n$config = json_decode(file_get_contents('config.json'));\r\n$validator = new Validator; $validator-&gt;validate(\r\n\t$config,\r\n\t(object)&#x5B;'$ref' =&gt; 'file:\/\/' . realpath('schema.json')],\r\n\tConstraint::CHECK_MODE_APPLY_DEFAULTS\r\n);\r\n\r\nif ($validator-&gt;isValid()) {\r\n\techo &quot;JSON validates OK\\n&quot;;\r\n} else {\r\n\techo &quot;JSON validation errors:\\n&quot;;\r\n\tforeach ($validator-&gt;getErrors() as $error) {\r\n\t\tprint_r($error);\r\n\t}\r\n}\r\n\r\nprint &quot;\\nResulting config:\\n&quot;;\r\nprint_r($config);\r\n<\/pre>\n<p>When I run it, I get the following output:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n$ php index.php \r\nJSON validates OK\r\n\r\nResulting config:\r\nstdClass Object\r\n(\r\n    &#x5B;blah] =&gt; foobar\r\n    &#x5B;foo] =&gt; bar\r\n    &#x5B;version] =&gt; v1.0.0\r\n)\r\n<\/pre>\n<p>What if I change my <em>config.json<\/em> to have something invalid, like an integer instead of a string?<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n\t&quot;blah&quot;: 1,\r\n\t&quot;foo&quot;: &quot;bar&quot;\r\n}\r\n<\/pre>\n<p>The validation fails with a helpful information:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n$ php index.php \r\nJSON validation errors:\r\nArray\r\n(\r\n    &#x5B;property] =&gt; blah\r\n    &#x5B;pointer] =&gt; \/blah\r\n    &#x5B;message] =&gt; Integer value found, but a string is required\r\n    &#x5B;constraint] =&gt; type\r\n)\r\n\r\nResulting config:\r\nstdClass Object\r\n(\r\n    &#x5B;blah] =&gt; 1\r\n    &#x5B;foo] =&gt; bar\r\n    &#x5B;version] =&gt; v1.0.0\r\n)\r\n<\/pre>\n<p>This is great. Maybe even beyond great!<\/p>\n<p>The possibilities here are endless. \u00a0First of all, we can obviously validate the configuration files. \u00a0Secondly, we can automatically generate the documentation for the supported configuration options and values. \u00a0It&#8217;s probably not going to be super fantastic at first, but it will cover ALL supported cases and will always be up-to-date. \u00a0Thirdly, this whole thing can be taken to the next level very easily, since the schema files are JSON themselves, which means schema&#8217;s can be generated on the fly.<\/p>\n<p>For example, in our projects, we allow the admin\/developer to specify which database field of a table is used as display field (in links and such). \u00a0Only existing database fields should be allowed. \u00a0So, we can generate the schema with available fields on project deployment, and then validate the user configuration against his particular database setup.<\/p>\n<p>There are probably even better ways to utilize all this, but I&#8217;ll have to think about it, which is not easy with the mind blown&#8230;<\/p>\n<p><strong>Update (March 16, 2017)<\/strong>: also have a look at some alternative JSON Schema validators. \u00a0<a href=\"https:\/\/github.com\/thephpleague\/json-guard\">JSON Guard<\/a>\u00a0might be a slightly better option.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). \u00a0Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. &#8230; &#8211; Installing seld\/jsonlint (1.6.0) &#8211; Installing justinrainbow\/json-schema (5.1.0) &#8230; Of course, I&#8217;ve heard of the seld\/jsonlint before. \u00a0It&#8217;s a &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Validating JSON against schema in PHP<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Validating JSON against schema in PHP #WebDev #PHP #JSON","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false,"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[3489,3195,3306,38,1366,1330],"keyring_services":[],"class_list":["post-27408","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-composer","tag-configuration-management","tag-json","tag-php","tag-validation","tag-web-development"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"description\" content=\"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=&quot;true&quot;] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course, I&#039;ve heard of the seld\/jsonlint\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Leonid Mamchenkov\"\/>\n\t<meta name=\"google-site-verification\" content=\"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Leonid Mamchenkov - Life, universe, and everything else\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Validating JSON against schema in PHP - Leonid Mamchenkov\" \/>\n\t\t<meta property=\"og:description\" content=\"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=&quot;true&quot;] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course, I&#039;ve heard of the seld\/jsonlint\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2017-03-11T09:55:22+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2017-03-16T07:32:15+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/MamchenkovBlog\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Validating JSON against schema in PHP - Leonid Mamchenkov\" \/>\n\t\t<meta name=\"twitter:description\" content=\"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=&quot;true&quot;] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course, I&#039;ve heard of the seld\/jsonlint\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@mamchenkov\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#blogposting\",\"name\":\"Validating JSON against schema in PHP - Leonid Mamchenkov\",\"headline\":\"Validating JSON against schema in PHP\",\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"},\"datePublished\":\"2017-03-11T11:55:22+02:00\",\"dateModified\":\"2017-03-16T09:32:15+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":9,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#webpage\"},\"articleSection\":\"All, Programming, Technology, Web work, composer, configuration management, JSON, PHP, validation, web development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/#listItem\",\"name\":\"Programming\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/#listItem\",\"position\":3,\"name\":\"Programming\",\"item\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#listItem\",\"name\":\"Validating JSON against schema in PHP\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#listItem\",\"position\":4,\"name\":\"Validating JSON against schema in PHP\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/category\\\/technology\\\/programming\\\/#listItem\",\"name\":\"Programming\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/\",\"name\":\"Leonid Mamchenkov\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"Leonid Mamchenkov\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#webpage\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/\",\"name\":\"Validating JSON against schema in PHP - Leonid Mamchenkov\",\"description\":\"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=\\\"true\\\"] ... - Installing seld\\\/jsonlint (1.6.0) - Installing justinrainbow\\\/json-schema (5.1.0) ... [\\\/code] Of course, I've heard of the seld\\\/jsonlint\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/2017\\\/03\\\/11\\\/validating-json-against-schema-in-php\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/author\\\/leonid\\\/#author\"},\"datePublished\":\"2017-03-11T11:55:22+02:00\",\"dateModified\":\"2017-03-16T09:32:15+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#website\",\"url\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/\",\"name\":\"Blog of Leonid Mamchenkov\",\"description\":\"Life, universe, and everything else\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mamchenkov.net\\\/wordpress\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Validating JSON against schema in PHP - Leonid Mamchenkov","description":"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=\"true\"] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course, I've heard of the seld\/jsonlint","canonical_url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"VHvdD0_usx1_4DzKy_QCVcICVgX2EgA2ybELT-wl7kQ","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#blogposting","name":"Validating JSON against schema in PHP - Leonid Mamchenkov","headline":"Validating JSON against schema in PHP","author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"},"datePublished":"2017-03-11T11:55:22+02:00","dateModified":"2017-03-16T09:32:15+02:00","inLanguage":"en-US","commentCount":9,"mainEntityOfPage":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#webpage"},"isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#webpage"},"articleSection":"All, Programming, Technology, Web work, composer, configuration management, JSON, PHP, validation, web development"},{"@type":"BreadcrumbList","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","position":1,"name":"Home","item":"https:\/\/mamchenkov.net\/wordpress","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/#listItem","name":"Programming"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/#listItem","position":3,"name":"Programming","item":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#listItem","name":"Validating JSON against schema in PHP"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#listItem","position":4,"name":"Validating JSON against schema in PHP","previousItem":{"@type":"ListItem","@id":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/#listItem","name":"Programming"}}]},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/#person","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"Person","@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author","url":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/","name":"Leonid Mamchenkov","image":{"@type":"ImageObject","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/3cf6df002a284d78fb6e9d8222ca4d102e0832035ed6bc8447008bd234e131a4?s=96&d=identicon&r=g","width":96,"height":96,"caption":"Leonid Mamchenkov"}},{"@type":"WebPage","@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#webpage","url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/","name":"Validating JSON against schema in PHP - Leonid Mamchenkov","description":"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=\"true\"] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course, I've heard of the seld\/jsonlint","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#website"},"breadcrumb":{"@id":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/#breadcrumblist"},"author":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"creator":{"@id":"https:\/\/mamchenkov.net\/wordpress\/author\/leonid\/#author"},"datePublished":"2017-03-11T11:55:22+02:00","dateModified":"2017-03-16T09:32:15+02:00"},{"@type":"WebSite","@id":"https:\/\/mamchenkov.net\/wordpress\/#website","url":"https:\/\/mamchenkov.net\/wordpress\/","name":"Blog of Leonid Mamchenkov","description":"Life, universe, and everything else","inLanguage":"en-US","publisher":{"@id":"https:\/\/mamchenkov.net\/wordpress\/#person"}}]},"og:locale":"en_US","og:site_name":"Leonid Mamchenkov - Life, universe, and everything else","og:type":"article","og:title":"Validating JSON against schema in PHP - Leonid Mamchenkov","og:description":"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=&quot;true&quot;] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course, I've heard of the seld\/jsonlint","og:url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/","og:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:secure_url":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg","og:image:width":1024,"og:image:height":1024,"article:published_time":"2017-03-11T09:55:22+00:00","article:modified_time":"2017-03-16T07:32:15+00:00","article:publisher":"https:\/\/www.facebook.com\/MamchenkovBlog","twitter:card":"summary_large_image","twitter:site":"@mamchenkov","twitter:title":"Validating JSON against schema in PHP - Leonid Mamchenkov","twitter:description":"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=&quot;true&quot;] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course, I've heard of the seld\/jsonlint","twitter:creator":"@mamchenkov","twitter:image":"https:\/\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2026\/03\/leonid-sailing-beer.jpg"},"aioseo_meta_data":{"post_id":"27408","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2023-07-19 15:10:55","updated":"2026-01-15 12:47:44","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/\" title=\"Programming\">Programming<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tValidating JSON against schema in PHP\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/mamchenkov.net\/wordpress"},{"label":"Technology","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/"},{"label":"Programming","link":"https:\/\/mamchenkov.net\/wordpress\/category\/technology\/programming\/"},{"label":"Validating JSON against schema in PHP","link":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":28439,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/03\/19\/have-you-tried-composer-scripts-you-do-not-need-phing\/","url_meta":{"origin":27408,"position":0},"title":"Have you tried Composer Scripts? You DO not need Phing.","author":"Leonid Mamchenkov","date":"March 19, 2018","format":false,"excerpt":"\"Have you tried Composer Scripts? You may not need Phing.\" is a nice blog post showing how to use Composer scripts to solve simple build and deployment automation.\u00a0 There's plenty of good advice in there. However I have one issue with it.\u00a0 It's the \"You may not need Phing.\" part.\u00a0\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":44293,"url":"https:\/\/mamchenkov.net\/wordpress\/2019\/09\/23\/github-adds-php-and-composer-dependency-graphs\/","url_meta":{"origin":27408,"position":1},"title":"GitHub adds PHP and Composer dependency graphs","author":"Leonid Mamchenkov","date":"September 23, 2019","format":false,"excerpt":"Here are some great news from GitHub: Dependency graph support is now available for PHP repositories with Composer dependencies. You may see security alerts on your repositories as dependency graph support rolls out. When there\u2019s a published vulnerability on any of the Composer dependencies that your project lists in\u00a0composer.json\u00a0and\u00a0composer.lock\u00a0files, GitHub\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/09\/github-php-composer.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/09\/github-php-composer.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/09\/github-php-composer.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/09\/github-php-composer.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/09\/github-php-composer.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2019\/09\/github-php-composer.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":27605,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/05\/23\/yang-a-data-modeling-language-for-the-network-configuration-protocol-netconf\/","url_meta":{"origin":27408,"position":2},"title":"YANG &#8211; A Data Modeling Language for the Network Configuration Protocol (NETCONF)","author":"Leonid Mamchenkov","date":"May 23, 2017","format":false,"excerpt":"In the spirit of validating everything against a schema\u00a0(validating JSON, validating CSV), here is another option - YANG: YANG\u00a0is a data modeling language for the definition of data sent over the NETCONF network configuration protocol. The name is an acronym for \"Yet Another Next Generation\". The YANG data modeling language\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":18699,"url":"https:\/\/mamchenkov.net\/wordpress\/2013\/10\/23\/has-php-json-been-removed-in-php-5-5\/","url_meta":{"origin":27408,"position":3},"title":"Has PHP JSON been removed in PHP 5.5?","author":"Leonid Mamchenkov","date":"October 23, 2013","format":"link","excerpt":"Has PHP JSON been removed in PHP 5.5?","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28653,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/06\/30\/php-jsonq-a-simple-elegant-php-package-to-query-over-any-type-of-json-data\/","url_meta":{"origin":27408,"position":4},"title":"php-jsonq &#8211; a simple, elegant PHP package to query over any type of JSON data","author":"Leonid Mamchenkov","date":"June 30, 2018","format":false,"excerpt":"php-jsonq provides an easy, yet powerful way to build queries for any JSON data (or PHP data structures for that matter, which are a step away).\u00a0 This has a variety of useful applications - data migration, API response filtering, complex configurations manipulation, and so on, and so forth.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27610,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/05\/23\/announcing-json-feed\/","url_meta":{"origin":27408,"position":5},"title":"Announcing JSON Feed","author":"Leonid Mamchenkov","date":"May 23, 2017","format":false,"excerpt":"Straight from the JSON Feed homepage: We \u2014\u00a0Manton Reece and Brent Simmons \u2014\u00a0have noticed that JSON has become the developers\u2019 choice for APIs, and that developers will often go out of their way to avoid XML. JSON is simpler to read and write, and it\u2019s less prone to bugs. So\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=27408"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27408\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=27408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=27408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=27408"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=27408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}