{"id":483737,"date":"2022-08-12T11:04:30","date_gmt":"2022-08-12T08:04:30","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-json-and-how-do-you-use-it\/"},"modified":"2022-08-12T11:04:30","modified_gmt":"2022-08-12T08:04:30","slug":"what-is-json-and-how-do-you-use-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/","title":{"rendered":"#What Is JSON and How Do You Use It?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a413168da4f0\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a413168da4f0\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#%E2%80%9CWhat_Is_JSON_and_How_Do_You_Use_It%E2%80%9D\" >&#8220;What Is JSON and How Do You Use It?&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#JSON_Basics\" >JSON Basics<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#A_Basic_JSON_Example\" >A Basic JSON Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#JSON_Data_Types\" >JSON Data Types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#Semantics_and_Validation\" >Semantics and Validation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#Designating_JSON_Content\" >Designating JSON Content<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#Working_With_JSON\" >Working With JSON<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#JavaScript\" >JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#PHP\" >PHP<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#Python\" >Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#Ruby\" >Ruby<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#JSON_Limitations\" >JSON Limitations<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#No_Schemas\" >No Schemas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#No_References\" >No References<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#No_Advanced_Data_Types\" >No Advanced Data Types<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#JSON_Alternatives\" >JSON Alternatives<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-json-and-how-do-you-use-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CWhat_Is_JSON_and_How_Do_You_Use_It%E2%80%9D\"><\/span>&#8220;What Is JSON and How Do You Use It?&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-816949\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/07\/shutterstock_1890188701.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Image of &quot;JSON&quot; text overlaid on a computer monitor\" width=\"1200\" height=\"675\"\/><figcaption class=\"wp-caption-text\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/json-inscription-against-laptop-code-background-1890188701\">Maria Vonotna\/Shutterstock.com<\/a><\/figcaption><\/figure>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.json.org\/json-en.html\">JSON (JavaScript Object Notation)<\/a> is a standardized format for representing structured data. Although JSON grew out of the Java<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a> programming language, it\u2019s now an ubiquitous method of data exchange between systems. Most modern-day APIs accept JSON requests and issue JSON responses so it\u2019s useful to have a good working knowledge of the format and its features.<\/p>\n<p>In this article, we\u2019ll explain what JSON is, how it expresses different data types, and the ways you can produce and consume it in popular programming languages. We\u2019ll also cover some of JSON\u2019s limitations and the alternatives that have emerged.<\/p>\n<h2 id=\"json-basics\"><span class=\"ez-toc-section\" id=\"JSON_Basics\"><\/span>JSON Basics<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>JSON was originally devised by <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.crockford.com\/qa2.html\">Douglas Crockford<\/a> as a stateless format for communicating data between browsers and servers. Back in the early 2000s, websites were beginning to asynchronously fetch extra data from their server, after the initial page load. As a text-based format derived from JavaScript, JSON made it simpler to fetch and consume data within these applications. The specification was eventually standardized as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.ecma-international.org\/publications\/files\/ECMA-ST\/ECMA-404.pdf\">ECMA-404 in 2013<\/a>.<\/p>\n<p>JSON is always transmitted as a string. These strings can be decoded into a range of basic data types, including numbers, booleans, arrays, and objects. This means object hierarchies and relationships can be preserved during transmission, then reassembled on the receiving end in a way that\u2019s appropriate for the programming environment.<\/p>\n<h2 id=\"a-basic-json-example\"><span class=\"ez-toc-section\" id=\"A_Basic_JSON_Example\"><\/span>A Basic JSON Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This is a JSON representation of a blog post:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"id\": 1001,&#13;\n    \"title\": \"What is JSON?\",&#13;\n    \"author\": {&#13;\n        \"id\": 1,&#13;\n        \"name\": \"James Walker\"&#13;\n    },&#13;\n    \"tags\": [\"api\", \"json\", \"programming\"],&#13;\n    \"published\": false,&#13;\n    \"publishedTimestamp\": null&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This example demonstrates all the JSON data types. It also illustrates the concision of JSON-formatted data, one of the characteristics that\u2019s made it so appealing for use in APIs. In addition, JSON is relatively easy to read as-is, unlike more verbose formats <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/XML\/XML_introduction\">such as XML<\/a>.<\/p>\n<h2 id=\"json-data-types\"><span class=\"ez-toc-section\" id=\"JSON_Data_Types\"><\/span>JSON Data Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Six types of data can be natively represented in JSON:<\/p>\n<ul>\n<li><strong>Strings<\/strong> \u2013 Strings are written between double quotation marks; characters may be escaped using backslashes.<\/li>\n<li><strong>Numbers<\/strong> \u2013 Numbers are written as digits without quotation marks. You can include a fractional component to denote a float. Most JSON parsing implementations assume an integer when there\u2019s no decimal point present.<\/li>\n<li><strong>Booleans<\/strong> \u2013 The literal values <code>true<\/code> and <code>false<\/code> are supported.<\/li>\n<li><strong>Null<\/strong> \u2013 The <code>null<\/code> literal value can be used to signify an empty or omitted value.<\/li>\n<li><strong>Arrays<\/strong> \u2013 An array is a simple list denoted by square brackets. Each element in the list is separated by a comma. Arrays can contain any number of items and they can use all the supported data types.<\/li>\n<li><strong>Objects<\/strong> \u2013 Objects are created by curly brackets. They\u2019re a collection of key-value pairs where the keys are strings, wrapped in double quotation marks. Each key has a value that can take any of the available data types. You can nest objects to create cascading hierarchies. A comma must follow each value, signifying the end of that key-value pair.<\/li>\n<\/ul>\n<p>JSON parsers automatically convert these data types into structures appropriate to their language. You don\u2019t need to manually cast <code>id<\/code> to an integer, for example. Parsing the entire JSON string is sufficient to map values back to their original data format.<\/p>\n<h2 id=\"semantics-and-validation\"><span class=\"ez-toc-section\" id=\"Semantics_and_Validation\"><\/span>Semantics and Validation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>JSON has certain rules that need to be respected when you encode your data. Strings that don\u2019t adhere to the syntax won\u2019t be parseable by consumers.<\/p>\n<p>It\u2019s particularly important to pay attention to quotation marks around strings and object keys. You must also ensure a comma\u2019s used after each entry in an object or array. JSON <em>doesn\u2019t<\/em> allow a trailing comma after the last entry though \u2013 unintentionally including one is a common cause of validation errors. Most text editors will highlight syntax problems for you, helping to uncover mistakes.<\/p>\n<p>Despite these common <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/trip-and-travel\/\" data-internallinksmanager029f6b8e52c=\"10\" title=\"Trip &amp; Travel\" target=\"_blank\" rel=\"noopener\">trip<\/a> points, JSON is one of the easiest data formats to write by hand. Most people find the syntax quick and convenient once they gain familiarity with it. Overall JSON tends to be less error-prone than XML, where mismatched opening and closing tags, invalid schema declarations, and character encoding problems often cause issues.<\/p>\n<h2 id=\"designating-json-content\"><span class=\"ez-toc-section\" id=\"Designating_JSON_Content\"><\/span>Designating JSON Content<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>.json<\/code> extension is normally used when JSON is saved to a file. JSON content has the standardized MIME type <code>application\/json<\/code>, although <code>text\/json<\/code> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/stackoverflow.com\/questions\/477816\/what-is-the-correct-json-content-type\">is sometimes used<\/a> for compatibility reasons. Nowadays you should rely on <code>application\/json<\/code> for <code>Accept<\/code> and <code>Content-Type<\/code> HTTP headers.<\/p>\n<p>Most APIs that use JSON will encapsulate everything in a top-level object:<\/p>\n<p>This isn\u2019t required though \u2013 a literal type is valid as the top-level node in a file, so the following examples are all valid JSON too:<\/p>\n<p>They\u2019ll decode to their respective scalars in your programming language.<\/p>\n<h2 id=\"working-with-json\"><span class=\"ez-toc-section\" id=\"Working_With_JSON\"><\/span>Working With JSON<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Most programming languages have built-in JSON support. Here\u2019s how to interact with JSON data in a few popular environments.<\/p>\n<h3 id=\"javascript\"><span class=\"ez-toc-section\" id=\"JavaScript\"><\/span>JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In JavaScript the <code>JSON.stringify()<\/code> and <code>JSON.parse()<\/code> methods are used to encode and decode JSON strings:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"javascript\">\n<pre class=\"de1\"><span class=\"kw1\">const<\/span> post <span class=\"sy0\">=<\/span> <span class=\"br0\">{<\/span>\n    id<span class=\"sy0\">:<\/span> <span class=\"nu0\">1001<\/span><span class=\"sy0\">,<\/span>\n    title<span class=\"sy0\">:<\/span> <span class=\"st0\">\"What Is JSON?\"<\/span><span class=\"sy0\">,<\/span>\n    author<span class=\"sy0\">:<\/span> <span class=\"br0\">{<\/span>\n        id<span class=\"sy0\">:<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>\n        name<span class=\"sy0\">:<\/span> <span class=\"st0\">\"James Walker\"<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"kw1\">const<\/span> encodedJson <span class=\"sy0\">=<\/span> JSON.<span class=\"me1\">stringify<\/span><span class=\"br0\">(<\/span>post<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ {\"id\": 1001, \"title\": \"What Is JSON?\", ...}<\/span>\nconsole.<span class=\"me1\">log<\/span><span class=\"br0\">(<\/span>encodedJson<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"kw1\">const<\/span> decodedJson <span class=\"sy0\">=<\/span> JSON.<span class=\"me1\">parse<\/span><span class=\"br0\">(<\/span>encodedJson<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ James Walker<\/span>\nconsole.<span class=\"me1\">log<\/span><span class=\"br0\">(<\/span>decodedJson.<span class=\"me1\">author<\/span>.<span class=\"me1\">name<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"php\"><span class=\"ez-toc-section\" id=\"PHP\"><\/span>PHP<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The equivalent functions in PHP are <code>json_encode()<\/code> and <code>json_decode()<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$post<\/span> <span class=\"sy0\">=<\/span> <span class=\"br0\">[<\/span>\n    <span class=\"st0\">\"id\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"nu0\">1001<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"title\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">\"What Is JSON?\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"author\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">[<\/span>\n        <span class=\"st0\">\"id\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"st0\">\"name\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">\"James Walker\"<\/span>\n    <span class=\"br0\">]<\/span>\n<span class=\"br0\">]<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$encodedJson<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw3\">json_encode<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$post<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ {\"id\": 1001, \"title\": \"What Is JSON?\", ...}<\/span>\n<span class=\"kw1\">echo<\/span> <span class=\"re0\">$encodedJson<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$decodedJson<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw3\">json_decode<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$encodedJson<\/span><span class=\"sy0\">,<\/span> <span class=\"kw4\">true<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ James Walker<\/span>\n<span class=\"kw1\">echo<\/span> <span class=\"re0\">$decodedJson<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"author\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"name\"<\/span><span class=\"br0\">]<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"python\"><span class=\"ez-toc-section\" id=\"Python\"><\/span>Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Python provides <code>json.dumps()<\/code> and <code>json.loads()<\/code> to serialize and deserialize respectively:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"python\">\n<pre class=\"de1\"><span class=\"kw1\">import<\/span> json\n\u00a0\npost <span class=\"sy0\">=<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"st0\">\"id\"<\/span>: <span class=\"nu0\">1001<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"title\"<\/span>: <span class=\"st0\">\"What Is JSON?\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"author\"<\/span>: <span class=\"br0\">{<\/span>\n        <span class=\"st0\">\"id\"<\/span>: <span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"st0\">\"name\"<\/span>: <span class=\"st0\">\"James Walker\"<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\nencodedJson <span class=\"sy0\">=<\/span> json.<span class=\"me1\">dumps<\/span><span class=\"br0\">(<\/span>post<span class=\"br0\">)<\/span>\n\u00a0\n<span class=\"co1\"># {\"id\": 1001, \"title\": \"What Is JSON?\", ...}<\/span>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>encodedJson<span class=\"br0\">)<\/span>\n\u00a0\ndecodedJson <span class=\"sy0\">=<\/span> json.<span class=\"me1\">loads<\/span><span class=\"br0\">(<\/span>encodedJson<span class=\"br0\">)<\/span>\n\u00a0\n<span class=\"co1\"># James Walker<\/span>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>decodedJson<span class=\"br0\">[<\/span><span class=\"st0\">\"author\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"name\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"ruby\"><span class=\"ez-toc-section\" id=\"Ruby\"><\/span>Ruby<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Ruby offers <code>JSON.generate<\/code> and <code>JSON.parse<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"ruby\">\n<pre class=\"de1\"><span class=\"kw3\">require<\/span> <span class=\"st0\">\"json\"<\/span>\n\u00a0\npost = <span class=\"br0\">{<\/span>\n    <span class=\"st0\">\"id\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"nu0\">1001<\/span>,\n    <span class=\"st0\">\"title\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">\"What Is JSON?\"<\/span>,\n    <span class=\"st0\">\"author\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"st0\">\"id\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"nu0\">1<\/span>,\n        <span class=\"st0\">\"name\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">\"James Walker\"<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\nencodedJson = JSON.<span class=\"me1\">generate<\/span><span class=\"br0\">(<\/span>post<span class=\"br0\">)<\/span>\n\u00a0\n<span class=\"co1\"># {\"id\": 1001, \"title\": \"What Is JSON?\", ...}<\/span>\n<span class=\"kw3\">puts<\/span> encodedJson\n\u00a0\ndecodedJson = JSON.<span class=\"me1\">parse<\/span><span class=\"br0\">(<\/span>encodedJson<span class=\"br0\">)<\/span>\n\u00a0\n<span class=\"co1\"># James Walker<\/span>\n<span class=\"kw3\">puts<\/span> decodedJson<span class=\"br0\">[<\/span><span class=\"st0\">\"author\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"name\"<\/span><span class=\"br0\">]<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"json-limitations\"><span class=\"ez-toc-section\" id=\"JSON_Limitations\"><\/span>JSON Limitations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>JSON is a lightweight format that\u2019s focused on conveying the values within your data structure. This makes it quick to parse and easy to work with but means there are drawbacks that can cause frustration. Here are some of the biggest problems.<\/p>\n<p>JSON data can\u2019t include comments. The lack of annotations reduces clarity and forces you to put documentation elsewhere. This can make JSON unsuitable for situations such as config files, where modifications are infrequent and the purposes of fields could be unclear.<\/p>\n<h3 id=\"no-schemas\"><span class=\"ez-toc-section\" id=\"No_Schemas\"><\/span>No Schemas<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>JSON doesn\u2019t let you define a schema for your data. There\u2019s no way to enforce that <code>id<\/code> is a required integer field, for example. This can lead to unintentionally malformed data structures.<\/p>\n<h3 id=\"no-references\"><span class=\"ez-toc-section\" id=\"No_References\"><\/span>No References<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Fields can\u2019t reference other values in the data structure. This often causes repetition that increases filesize. Returning to the blog post example from earlier, you could have a list of blog posts as follows:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"posts\": [&#13;\n        {&#13;\n            \"id\": 1001,&#13;\n            \"title\": \"What is JSON?\",&#13;\n            \"author\": {&#13;\n                \"id\": 1,&#13;\n                \"name\": \"James Walker\"&#13;\n            }&#13;\n        },&#13;\n        {&#13;\n            \"id\": 1002,&#13;\n            \"title\": \"What is SaaS?\",&#13;\n            \"author\": {&#13;\n                \"id\": 1,&#13;\n                \"name\": \"James Walker\"&#13;\n            }&#13;\n        }&#13;\n    ]&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Both posts have the same author but the information associated with that object has had to be duplicated. In an ideal world, JSON parser implementations would be able to produce the structure shown above from input similar to the following:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"posts\": [&#13;\n        {&#13;\n            \"id\": 1001,&#13;\n            \"title\": \"What is JSON?\",&#13;\n            \"author\": \"{{ .authors.james }}\"&#13;\n        },&#13;\n        {&#13;\n            \"id\": 1002,&#13;\n            \"title\": \"What is SaaS?\",&#13;\n            \"author\": \"{{ .authors.james }}\"&#13;\n        }&#13;\n    ],&#13;\n    \"authors\": {&#13;\n        \"james\": {&#13;\n            \"id\": 1,&#13;\n            \"name\": \"James Walker\"&#13;\n        }&#13;\n    }&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This is not currently possible with standard JSON.<\/p>\n<h3 id=\"no-advanced-data-types\"><span class=\"ez-toc-section\" id=\"No_Advanced_Data_Types\"><\/span>No Advanced Data Types<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The six supported data types omit many common kinds of value. JSON can\u2019t natively store dates, times, or geolocation points, so you need to decide on your own format for this information.<\/p>\n<p>This causes inconvenient discrepancies and edge cases. If your application handles timestamps as strings, like <code>2022-07-01T12:00:00+00:00<\/code>, but an external API presents time as seconds past the Unix epoch \u2013 <code>1657287000<\/code> \u2013 you\u2019ll need to remember when to use each of the formats.<\/p>\n<h2 id=\"json-alternatives\"><span class=\"ez-toc-section\" id=\"JSON_Alternatives\"><\/span>JSON Alternatives<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/yaml.org\">YAML<\/a> is the leading JSON alternative. It\u2019s a superset of the format that has a more human-readable presentation, custom data types, and support for references. It\u2019s intended to address most of the usability challenges associated with JSON.<\/p>\n<p>YAML has seen wide adoption for config files and within DevOps, IaC, and observability tools. It\u2019s less frequently used as a data exchange format for APIs. YAML\u2019s relative complexity means it\u2019s less approachable to newcomers. Small syntax errors can cause confusing parsing failures.<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developers.google.com\/protocol-buffers\">Protocol buffers (protobufs)<\/a> are another emerging JSON contender designed to serialize structured data. Protobufs have data type declarations, required fields, and support for most major programming languages. The system is gaining popularity as a more efficient way of transmitting data over networks.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>JSON is a text-based data representation format that can encode six different data types. JSON has become a staple of the software development ecosystem; it\u2019s supported by all major programming languages and has become the default choice for most REST APIs developed over the past couple of decade.<\/p>\n<p>While JSON\u2019s simplicity is part of its popularity, it also imposes limitations on what you can achieve with the format. The lack of support for schemas, comments, object references, and custom data types means some applications will find they outgrow what\u2019s possible with JSON. Younger alternatives such as YAML and Protobuf have helped to address these challenges, while XML remains a contender for applications that want to define a data schema and don\u2019t mind the verbosity.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/what-is-json-and-how-do-you-use-it\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;What Is JSON and How Do You Use It?&#8221; Maria Vonotna\/Shutterstock.com JSON (JavaScript Object Notation) is a standardized format for representing structured data. Although JSON grew out of the JavaScript programming language, it\u2019s now an ubiquitous method of data exchange between systems. Most modern-day APIs accept JSON requests and issue JSON responses so it\u2019s useful&#8230;<\/p>\n","protected":false},"author":1,"featured_media":483738,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/07\/shutterstock_1890188701.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-483737","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/483737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=483737"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/483737\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/483738"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=483737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=483737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=483737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}