What beginners should know about JSON

What beginners should know about JSON
Published: by Renier Delport
Collection(s): Coding, Website Development
Tagged with: , , , .

JSON (JavaScript Object Notation) is a lightweight syntax or standard for storing and exchanging data between servers and users. Data presented in JSON format is text based/human readable and will look something like this:

{
  "firstName": "Johan",
  "lastName": "Koekemoer",
  "address": {
   "streetAddress": "57 Church Street",
   "city": "Cape Town",
   "state": "Western Cape",
   "subburb": "Ocean View",
   "postalCode": "7975"
  },
  "phoneNumbers": [
   "021 765 1234",
   "083 240 4567"
  ]
}

This sample data set will be used for explanation purposes.

JSON example uses

JSON is a fast way for servers to present data to users. Being human-readable/text-based data is transferred without exposing servers or users to harm. The newest data becomes available almost as soon as it changed.  It is often used in services like APIs (Application Programming Interfaces) and web services that provide public data.

Weather stations

Open source weather stations commonly use JSON to present their data. This makes it easy for user computers and Apps to access weather specific data on their devices.

Dark Sky weather API

Dark Sky, for example, has a nice, free weather forecast service. By using a web browser and searching a location, weather data is presented on a web page. By using the their API, the same data can be obtained in JSON format. An API call to for example Church Street in Cape Town will give this data.

Data presentation (servers)

From the server side, the data can be generated into JSON format (also called parsing or encoding) and can be presented to users from a request URL on the server. The server can use data that is available on the server itself (e.g. a database, sensors, etc.) or data obtained from somewhere else.

For a server to be able to parse data in JSON format, software programming is used. Even though it closely resembles JavaScript object literal syntax, it can be used independently from JavaScript, and many programming environments feature the ability parse to JSON.

Programming gives the server complete control of what data to parse and how the data should be parsed. Although data is commonly set up to be accessible to anyone, servers also has the ability to limit the access by setting up rules (e.g. membership, subscription, passwords, access tokens or login status).

JSON syntax basics

In JSON, data is structured in a specific way. Symbols like { } , : “” and [ ] are used to represent different meanings. When, for example, a request for the sample data set is made, JSON will have the following basic syntax:

  • Data is represented in key/value sets:
"firstName": "Johan",
"lastName": "Koekemoer"
  • The colon, (:), assigns a value to key
  • Key/value pairs are separated with commas (,)
  • Curly brackets hold objects ({ })
"address": {
 "streetAddress": "57 Church Street",
 "city": "Cape Town",
 "state": "Western Cape",
 "subburb": "Ocean View",
 "postalCode": "7975"
}
  • Square brackets hold arrays ([ ])
"phoneNumbers": [ "021 765 1234", "083 240 4567" ]

Note how the entire set (called a JSON string) is also encapsulated by curly brackets and separated how each key/value set is separated by commas:

{
  "firstName": "Johan",
  "lastName": "Koekemoer",
  "address": {
   "streetAddress": "57 Church Street",
   "city": "Cape Town",
   "state": "Western Cape",
   "subburb": "Ocean View",
   "postalCode": "7975"
  },
  "phoneNumbers": [
   "021 765 1234",
   "083 240 4567"
  ]
}

In this example, firstName, lastName, address and phoneNumbers are all keys with their own values. The ‘address’ key contains multiple objects, each with its own key/value set and the ‘phoneNumbers’ key contains an array with data (in this case two telephone numbers).

In the example above, all values are presented as strings. JSON data can also be presented, for example, as integers, booleans:

"age": 43 ,
"married" : "true" ,
"firstBornName" : "null"

Data collection (users)

User computers or devices (with access) can use the server’s URL to request data in JSON format. The JSON formatted data can either be viewed in a web browser (see earlier), or it can be analysed and decoded with software or programming to be used more specifically. The requesting URL can also be used to tell the server more specifically what data is required, e.g. language input and amount of data.

By knowing the basic JSON syntax, users are able to make use of the data after their request has been processed. Data can either be obtained by using the key value itself and/or by using the sequential numbering of the key.

Making JSON requests

JSON data requests can be made in various ways. It is usually managed/controlled or through some sort of API. Simple requests can be made from web browsers, the terminal or various programming environments.

More website development related posts

Try one of Lynda.com’s online software and skills learning courses
Try one of Lynda.com’s online software and skills learning courses
Improve your knowledge on anything from programming to business skills with Lynda.com’s huge range of professional video courses.
23 February 2018

Save, share, comment & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a shit. Keep it constructive and polite.
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPin on PinterestShare on StumbleUpon

Comment via Facebook

Disqus

About the author
Renier finds himself busy with creative web design and his websites, motorcycling, photoshopping, micro electronics, non-commercialised music, superhero movies, badass series and many other things that are not interesting to most people.