{"id":12111,"date":"2015-08-08T16:32:21","date_gmt":"2015-08-08T16:32:21","guid":{"rendered":"https:\/\/www.bartbusschots.ie\/s\/?p=12111"},"modified":"2015-08-09T00:33:50","modified_gmt":"2015-08-09T00:33:50","slug":"json-a-quick-intro","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2015\/08\/08\/json-a-quick-intro\/","title":{"rendered":"JSON &#8211; a quick intro"},"content":{"rendered":"<p>The letters JSON stand for <em>JavaScript Object Notation<\/em>, but that doesn&#8217;t really help you understand what it is, and what it&#8217;s for. JSON may have it&#8217;s origins in JavaScript, but it&#8217;s moved on from there. JSON has become a very popular human-readable plain-text format for sending structured data between applications.<\/p>\n<p>In short, JS is a way of representing data that both computers and humans can read and understand. Think of it as XML&#8217;s younger better looking cousin \ud83d\ude42<\/p>\n<p>Note that this article is intended to be a JSON primer, not an exhaustive description of the format.<\/p>\n<p><!--more--><\/p>\n<h2>Three Building Blocks<\/h2>\n<p>At it&#8217;s simplest level, JSON has just three building blocks &#8211; single values, lists of values, and sets of name-value pairs. Using just these three simple building blocks, JSON can represent very complex data structures. Let&#8217;s start by looking at each one individually:<\/p>\n<h3>Single Values<\/h3>\n<p>A single value is just one piece of data. It can be a number, a string of characters, or a boolean (true\/false) value.<\/p>\n<p>Numbers can be integers or floating point numbers, and are represented in JSON without any additional fluff &#8211; the following are all valid JSON numeric values:<\/p>\n<pre>\r\n42\r\n1001\r\n-33\r\n3.1415\r\n1256.56\r\n-42.7\r\n<\/pre>\n<p>There are two valid boolean values in JSON:<\/p>\n<pre>\r\ntrue\r\nfalse\r\n<\/pre>\n<p>Strings of characters must be enclosed within quotation marks<\/p>\n<pre>\r\n\"This is a string\"\r\n<\/pre>\n<p>The backslash character (<code>\\<\/code>) is used to &#8216;escape&#8217; special values within a string, e.g.:<\/p>\n<ul>\n<li><code>\\n<\/code> represents a new line character<\/li>\n<li><code>\\t<\/code> represents a tab character<\/li>\n<li><code>\\\"<\/code> represents a quotation mark<\/li>\n<li><code>\\\\<\/code> represents a single backslash<\/li>\n<\/ul>\n<p>JSON strings should always be encoded in UTF-8, so you can include accented characters in your strings directly:<\/p>\n<pre>\r\n\"This clich\u00c3\u00a9 of a string is valid\"\r\n\"this string\\nis split over two lines\"\r\n\"you can even include emoji in JSON strings: \u00f0\u0178\u2019\u00a9\"\r\n<\/pre>\n<h3>Lists<\/h3>\n<p>A list is, as it&#8217;s name suggests, an ordered sequence of one or more values. In programmer-speak, they are arrays.<\/p>\n<p>JSON represents an array as a comma-separated sequence of values enclosed between <code>[<\/code> and <code>]<\/code> characters. The following are valid JSON lists:<\/p>\n<pre>\r\n[1, 2, 3, 4, 5]\r\n[\"hello there, I am one value\", true, -3.1415, 22, \"boo!\"]\r\n<\/pre>\n<h3>Dictionaries<\/h3>\n<p>A dictionary is a collection of name-value pairs, where the name is a string, and the value can be any valid JSON value. Programers might be more used to the terms Hash table, or hash reference for this kind of data structure.<\/p>\n<p>In JSON, the name and value which make up a single name-value pair are separated by the <code>:<\/code> character, and the pairs that make up the dictionary separated by commas. The entire dictionary is enclosed between <code>{<\/code> and <code>}<\/code> characters.<\/p>\n<pre>\r\n{\"name\" : \"Bart's Widget\", \"price\" : 23.45, \"currency\" : \"\u00e2\u201a\u00ac\"}\r\n<\/pre>\n<h2>Building Bigger<\/h2>\n<p>Both lists and dictionaries store values, and, those values can be ANY valid JSON value, including lists and dictionaries. The support for nesting it what makes JSON so powerful.<\/p>\n<p>As an example, the <code>Crypt::HSXKPasswd<\/code> perl module represents it&#8217;s configuration as a collection of name-value pairs, where some of those values are lists if strings. This means that JSON can be used to represent any valid <code>Crypt::HSXKPasswd<\/code> configuration, e.g.:<\/p>\n<pre>\r\n{\r\n    \"num_words\": 3,\r\n    \"word_length_min\": 5,\r\n    \"word_length_max\": 7,\r\n    \"case_transform\": \"RANDOM\",\r\n    \"separator_character\": \"RANDOM\",\r\n    \"separator_alphabet\": [\r\n        \"-\",\r\n        \":\",\r\n        \".\",\r\n        \",\"\r\n    ],\r\n    \"padding_digits_before\": 2,\r\n    \"padding_digits_after\": 2,\r\n    \"padding_type\": \"FIXED\",\r\n    \"padding_character\": \"RANDOM\",\r\n    \"symbol_alphabet\": [\r\n        \"!\",\r\n        \"?\",\r\n        \"@\",\r\n        \"&\"\r\n    ],\r\n    \"padding_characters_before\": 1,\r\n    \"padding_characters_after\": 1,\r\n    \"random_increment\": \"AUTO\"\r\n}\r\n<\/pre>\n<p>Note that dictionaries and lists can be spread over multiple lines, allowing for nice human-readable data structures like the one above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The letters JSON stand for JavaScript Object Notation, but that doesn&#8217;t really help you understand what it is, and what it&#8217;s for. JSON may have it&#8217;s origins in JavaScript, but it&#8217;s moved on from there. JSON has become a very popular human-readable plain-text format for sending structured data between applications. In short, JS is a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","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},"categories":[12,16],"tags":[540,458],"series":[],"class_list":["post-12111","post","type-post","status-publish","format-standard","hentry","category-computers-tech","category-programming","tag-json","tag-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-39l","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/12111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=12111"}],"version-history":[{"count":3,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/12111\/revisions"}],"predecessor-version":[{"id":12120,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/12111\/revisions\/12120"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=12111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=12111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=12111"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=12111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}