Skip to content

What's a JSON file

jjppof edited this page Jan 18, 2023 · 1 revision

What's a JSON file

JSON file is a file that will hold data organized in a standard way. In short, JSON is a way to store data. That's all, it's not a programming language.

JSON consists of various brackets, commas, and double quotes. And mostly, JSON structures will bring data in pairs, like key-values (just like dictionaries). Example:

{
  "key": "value"
}

Whenever you have data enclosed by curly braces, you have an object. So, above, the outer curly brackets wrap around the whole object to make sure all the data stays together in one object. The left text string, or "key" is simply a name and can be any valid name. Keys are always enclosed by double quotes.

Data types

JSON supports different types of data when defining the values. They are object, array, string, number, boolean, and null.

{
  "string": "some text",
  "number": 5.67,
  "boolean": true,
  "empty": null
}
  • A string is a set of characters, or simply, text.
  • A number is a straightforward number without double quotes around it.
  • A boolean can be either true or false. Note that it is written in all lowercase and without double quotes.
  • A null value is an empty value. This might be useful if the value isn’t known yet but the object might need to contain a specific key so that all objects have the same keys.

Also, note the commas after each value. This lets JSON know that it is looking at the next value. This is needed because JSON files can be minified to reduce disk space. In most cases, minifying will remove all linebreaks and spaces so letting JSON know what is the next value is very important.

Arrays

Arrays, or lists, are a very useful data type that holds some data together in sequence. A list is made with brackets [ and ]. You can put any value in between and separate them with commas. Example:

{
  "name": "John Doe",
  "grades": [5, 6, 5.5, 1, 7, 7.5],
  "passed": true
}

Please notice that the array items can be of any type, including objects and even other arrays:

{
  "name": "John Johnsson",
  "studentNumber": "09846115",
  "classes": [
    {
      "name": "Agriculture",
      "points": 4,
      "year": 2021
    },
    {
      "name": "Computer science",
      "points": 4,
      "year": 2021
    }
  ]
}

Objects

We can define JSON objects like we have made so far within JSON objects. This is called nesting.

{
  "name": "John Doh",
  "course": {
    "name": "Information Technologies",
    "points": 4,
    "year": 2020,
    "retake": false
  }
}

Here we create an object of another student with a familiar name. The student follows a course. This course can be an object (which it is in this example) with more key-value pairs like we’ve done before.

Notice the curly brackets we use within our object to create a nested object.

Conclusion

JSON is a way of storing data in .json files. It is much more advanced and more powerful than CSV or Excel sheets. The syntax can be harder to learn but once you get the hang of it you will never forget.

Reference: JSON in plain English

Clone this wiki locally