JSON Data Example

What is JSON?

JSON (JavaScript Object Notation) is a widely used lightweight textual data interchange format based on JavaScript. Like many other text-based formats, JSON is easy for humans to read and easy for computers to parse and process. Despite its origins in JavaScript, it is considered to be a programming language independent. For many programming languages, there are ready-to-use code libraries for creating and manipulating JSON data. Due to its conciseness compared to XML, JSON can be more suitable for serializing and storing complex data structures, especially if the data size is important. JSON is smaller and more convenient than XML.

JSON is often used to transfer data over the Internet. The JSON is used by web applications (web and mobile apps) for both client-server and server-server (HTTP software bridging) communications to exchange data between a client and a server via REST API.

JSON Example

The following example shows an object describing a person in JSON format.

Sample JSON for a person
{
  "firstName": "John",
  "lastName": "Smith",
  "address": {
      "streetAddress": "5th Avenue, 101",
      "city": "New York",
      "postalCode": 10001
  },
  "phoneNumbers": [
      "800-123-4444",
      "800-123-5555"
  ]
}

JSON Structure

JSON data can be stored in one of two structures:

1) A set of key:value pairs. In various programming languages, this is implemented in the form of a record, structure, dictionary, hash table, keyed list, or an associative array. The key can only be a string. The case sensitivity for the key is not regulated by the standard and depends on the specific software that uses the JSON data.

Names with letters in different cases are considered different. Duplicate key names are allowed but not recommended. The handling of such situations is at the discretion of the software using the JSON. Possible options are to consider only the first key, consider only the last key, or generate an error. For example JavaScript throws an error when parsing a JSON with duplicate keys.

JSON data example with key:value pairs
{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}

2) An ordered set of values. In many programming languages, this is implemented as an array, vector, list, or sequence.

JSON data example with array
{
  "items": [	
     {"key1": "value1"},
     {"key2": "value2"},
     {"key3": "value3"}
  ]
}

Both JSON data structures are supported by most modern programming languages, which allows the JSON format to be used to exchange data between clients and servers written in different programming languages and running on different infrastructures.

JSON Syntax Rules

  • objects are enclosed in curly braces {}.
  • arrays are enclosed in square brackets [].
  • numbers are written without quotes.
  • literals true, false and null are written without quotes.
  • strings must be enclosed in double quotes.
  • single quotes for strings are not allowed.
  • unicode characters are allowed in strings.
  • special characters must be escaped with a backslash or written in hexadecimal unicode as \uFFFF.

JSON strings are like JavaScript strings. JSON numbers are also similar to JavaScript numbers, except that JSON uses only dotted decimal format. Spaces can be inserted between any two syntax elements.

JSON Data Values

Example of JSON Data Values
{
    "address": {
        "address": "5th Avenue, New York",
        "postalCode": 10001
    }
}

You can use both numbers and strings as values in JSON. In this JSON Data Values example, the "address" contains a string, and "postalCode" contains a numeric value. Due to weak typing in JavaScript, a string can be converted to a number and does not affect the logic of the program, but in some situations, this can lead to a problem when the code expects a number but receives strings. It is recommended that you handle the value type with caution because JSON is used for cross-system communication.

JSON Requests and Responses

Clients sending JSON data to the server must include the Content-Type: application/json request header in the request. This header allows the server to process the data in the request message body correctly. If clients expect the server to return JSON, it must also send the Accept: application/json header to inform the server about it.

See also

Updated: Viewed: 10110 times