What beginners should know about JSON

What beginners should know about JSON

JSON is a lightweight syntax or standard for storing and exchanging data between servers and users. Data presented in JSON format is human-readable.

Introduction to JSON

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. Human-readable/text-based data is transferred without exposing servers or users to harm. The newest data become 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 using computers and Apps to access weather-specific data on their devices.

Dark Sky weather API

The Dark Sky weather API.

Dark Sky, for example, has an excellent, free weather forecast service. By using a web browser and searching a location, weather data is presented on a web page. By using 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 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 to parse to JSON.

Programming gives the server complete control of what data to parse and how the data should be parsed. Although data are commonly set up to be accessible to anyone, servers also have the ability to limit 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 of 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.

Conclusion

JSON is a lightweight syntax storing and exchanging data between servers and users.

Leave a Reply

Your email address will not be published. Required fields are marked *