Masthead

The JSON Standard

Introduction

JSON is a relatively new standard that is being heavily used on for web programming. JSON standard for "JavaScript Object Notation". JSON has a structure that is very close the Python dictionaries and can contain the same type of data as XML.

The Structure of JSON

All JSON data starts with an opening brace and ends with an ending brace. Then, there is a series of key/value pairs. Each value can be a single value or a series of key/value pairs.

JSON has key/value pairs similar to XML but they are formatted a bit differently. Below is an example of some key/value pairs in JSON showing how JSON encodes strings, boolean values, integers, and floating point values (from Wikipedia). Note that colons are used to separate the key from the value and commas are used to separate each of the key/value pairs.

{
  "firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"height_cm": 167.6, }

JSON values can be arrays of key value pairs allowing it to encode a large amount of complex information very quickly and simply. Below is an example of how a variable can contain a complex value with different types of data encoded into it by using braces within the value (Wikipedia).

{
  "address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
} }

You can also add brackets to add multiple items into an "array" for a value.

{
	"phoneNumbers": [
		{
			"type": "home",
			"number": "212 555-1234"
		},
		{
			"type": "office",
			"number": "646 555-4567"
		}
	]
}

There is a little more to JSON but that is most of what you will need.

Formatting

Remember to properly tab your JSON files even if you are writing them out from code so that you and others can read them more easily and debug them.

Additional Resources

Wikipedia JSON Page

© Copyright 2018 HSU - All rights reserved.