👨‍💻 Studentbox documentation

Search

Search IconIcon to open search

JSON

Last updated Jan 28, 2023 Edit Source

From the official website

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

Introducing json (no date) JSON. Available at: https://www.json.org/json-en.html (Accessed: January 13, 2023).

It’s a serialisation format that is human-friendly, meaning objects, arrays and values are represented in a string. Example:

1
2
3
4
5
6
7
{
  "name": "Simon",
  "surname": "Leonard",
  "age": 21,
  "hobbies": ["cooking", "reading", "drinking coffee"],
  "enemies": null
}

Here is a speculative object that could represent me in JSON.

This time, let’s say I have a array of users:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
{
  "users": [
    {
      "id": "12345",
      "name": "Simon",
      "surname": "Leonard",
      "age": 21,
      "hobbies": ["cooking", "reading", "drinking coffee"],
      "allies": ["54321"]
    },
    {
      "id": "54321",
      "name": "Lucas",
      "surname": "Thomas",
      "age": 22,
      "hobbies": [],
      "allies": ["12345"]
    }
  ]
}

We can see that JSON has at least one drawback: the document’s structure and the data are mixed. You can see above that we repeat the keys id, name, surname… for multiple users.

Is it possible to transfer a file over JSON?

Although protocols like REST and gRPC are more suitable for the task (with multipart upload and stream respectively), one could upload a file by encoding it in base64. Note that it increase the data size by ~33%1


  1. T, D. and B, S. (2019) Posting a file and associated data to a restful webservice preferably as JSON, Stack Overflow. Available at: https://stackoverflow.com/a/4083908 (Accessed: January 5, 2023). ↩︎