What is JSON?
JSON is an acronym for Javascript Object Notation. JSON is a lightweight, open-standard data interchange format designed especially for human-readable data interchange. JSON is the standard in passing requests and responses in REST APIs and web applications. JSON is self-describing. A human or a machine can read JSON data and understand it, and it is less verbose, faster, and more readable than XML. The main contrast between JSON vs XML, is that JSON follows a compact style to improve its readability. JSON uses key-value pairs to improve extensibility. JSON is not a programming language.
JSON Format
Here's an example of a JSON file describing an array called "employees" containing three JSON objects.
json{"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"}]}
json { "employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ] }
Notice the key-value pairs: `firstName` being a key, and "John" being its value. We also see some carry-over from JavaScript data structures, as the arrays are inside square brackets, and objects are inside curly braces.
How do I use JSON?
JSON is the format used in the API response when you send a GET request for a record. Here is a curl request and a JSON example:
```curl 'https://holidays.abstractapi.com/v1/?api_key={YOUR API KEY}&country=US&year=2020&month=12&day=25'```
will return a JSON card with your requested information:
json [ { "name": "Christmas Day", "name_local": "", "language": "", "description": "", "country": "US", "location": "United States", "type": "National", "date": "12/25/2020", "date_year": "2020", "date_month": "12", "date_day": "25", "week_day": "Friday" } ]
This assumes you have `application/json` in the `Content-type` header of your request.
REST Client Request Format
The header of the request specifies what kind of content the client is expecting and able to receive from the server. This is called the Accept field. An example of a common accept field is `application/json`, specifying that it will receive a JSON file in the response. The server returns the requested data in the format requested in the request header. The request header will contain something like this (among other things, like the protocol and cache control):
: json GET https://holidays.abstractapi.com/v1/?api_key={YOUR API KEY}&country=US&year=2020&month=12&day=25 Content-Type: application/json
JSON Basic Data Types
- Number: Integer or floating-point number.
- String: a sequence of zero or more Unicode characters. Strings are delimited with double-quotation marks and support a backslash escaping syntax.
- Boolean: either of the values true or false.
- Array: an ordered list of zero or more elements, each of which may be of any type. Arrays use square bracket notation with comma-separated elements.
- JSON Object: a collection of name-value pairs where the names (also called keys) are strings. Each key is unique within an object. Objects are delimited with curly braces and use commas to separate each pair, while within each pair the colon ':' character separates the key or name from its value.
- Null: an empty value
Conclusion
JSON is a lightweight and open standard for human and machine-readable message exchange. Learning JSON syntax is an important part of your API programming journey, as it will allow you to orchestrate API requests and responses, understand their exchanges, and test them.