Skip to content

Instantly share code, notes, and snippets.

@benbalter
Created October 25, 2012 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbalter/3952577 to your computer and use it in GitHub Desktop.
Save benbalter/3952577 to your computer and use it in GitHub Desktop.
Into to JSON

JSON is a lightweight and simple way to represent machine-readable data. It is quickly becoming the defacto standard for shuttling data across the internet, fueled primarily by the rise of mobile and APIs. Most if not all modern programing languages can interpret and produce JSON out of the box.

The most basic element of JSON is a JSON object. JSON objects are bound by curly braces ( { and }). WIthin the object is a series of key, value pairs, both enclosed with double quotes and separated by a colon and a space. Individual elements within the object (each key/value pair) are separated by commas just as you would in english. For example, an object representing a dog may be:

{ "name": "Fido", "type": "Golden Retriever" }

Multiple objects are grouped into an array, denoted by square braces ( [ and ] ). Each object within the array is separated by a comma, just as you would in a list of english words. Thus a group of two dogs would be:

[ { "name": "Fido", "type": "Golden Retriever" }, { "name": "Rover", "type": "Black Lab" } ]

Note, for the most part, JSON doesn't care about formatting. Thus, { "name": "Fido", "type": "Golden Retriever" } is equivalent to:

{
"name": "Fido", 
"type": "Golden Retriever" 
}

Which may be easier to read. One gotcha: within quotes (e.g., the values), there are a few restrictions. You can't have any line breaks (new lines -- replace them with \n) and you can't have any double quotes (replace them with \"). Note, any mistake, even if a missing comma, will cause the entire file to be unreadable, so be sure to check your work.

Resources: Google "JSON Formatter" or "JSON Validator" to help make JSON readable (for your own use) or to check your work.

Bonus: Sometimes you'll see reference to JSONP. JSONP is a type of JSON, where the response from an API is wrapped in a keyword supplied by the application calling the API. This allows a more secure transport of the data, and is generally used in mobile and cross-site API requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment