Data structures

From unstructured to structured data

Data can take many forms that we can situate along a continuum with unstructured data at one extreme, structured data at the other, and semi-structured data in between. In this section, we briefly explore what these concepts mean.

Unstructured data

Let us consider a simple example to better grasp the difference between structured and unstructured data and, at the same time, to get a sense of the process of structuring data so that it can be more effectively strored and analyzed.

Here is a brilliant piece of writing:

A data story
Philippe Mongeon
Chapter 1
Once upon a time, a random internet user created a short text and abandoned it in a sea of unstructured data.
Chapter 2
An algorithm passed by and decided to add XML tags to the text.
Chapter 3
A data manager stumbled upon the data and thought it would be relevant for the database users. So they decided to add it to their database.
The end.

That’s unstructured data (not entirely, the line breaks are a very basic structure). We humans who have read a book before can see the structure. We see a title, an author name, and three chapters that contain some text. This is our brains structuring the data that our eyes capture. The computer does not perform that structuring process by itself. For it, this is just a bunch of symbols. In fact, for a computer this is all 1s and 0s (binary data) because that’s the only type of data computers actually stores and understands. Because the computers we use are made by and for humans, we have programmed them to show us symbols we understand.

Semi-structured data

Another way to think about the data above is that it has an implicit structure. But humans have invented different tools to make the structure of data explicit. One of these tools is XML (Extensible Markup Language). It is very similar to the Hypertext Markup Language (HTML), which web browers use to interpret the structure of a webpage (such as the one you are currently viewing). These tagging systems use tags to allow humans and computer to differentiate between different parts of the data, and to indicate what it represents (e.g., a title, an author, a paragraph, etc.)

Here is how, for example, we can apply XML tags to give our data some structure.

<book>
  <title>A data story</title>
  <author>Philippe Mongeon</author>
  <chapter>
    <title>Chapter 1</title>
    <paragraph>Once upon a time a short text was created by a random internet user
    and abandoned in a sea of unstructured data.</paragraph>
  </chapter>
  <chapter>
    <title>Chapter 2</title>
    <paragraph>An algorithm passed by and decided to add xml tags to the text.</paragraph>
  </chapter>
  <chapter>
    <title>Chapter 3</title>
    <paragraph>A data manager stumbled upon the data, thought it would be relevant
    for the database users, and so she decided to add it to include it in her
    database.</paragraph>
    <paragraph>The end.</paragraph>
  </chapter>
</book>

As you can see, the XML tags already help us (and the computer) identify different text components, such as the title, the author, headers and paragraphs. XML documents are a typical example of semi-structured data.

Another frequently used semi-structured format is JSON. It is a name-value pair format. We can use it to structure our text by identifying the different parts (the names) and their content (the values).

{
  "title": "A data story",
  "author": "Philippe Mongeon",
  "Chapter 1": "Once upon a time a short text was created by a random internet user and abandoned in a sea of unstructured data.",
  "Chapter 2": "An algorithm passed by and decided to add xml tags to the text.",
  "Chapter 3": "A data manager stumbled upon the data, thought it would be relevant for the database users, and so she decided to add it to her database. The end."
}

Notice how I included the Chapter number in the tags in the JSON example above? That was an arbitrary choice, and I could have chosen another structure for my file, such as this one:

{
  "title": "A data story",
  "author": "Philippe Mongeon",
  "Chapters": [
      {
        "title": "Chapter 1",
        "content": "Once upon a time a short text was created by a random internet user and abandoned in a sea of unstructured data."
      }
      {
        "title": "Chapter 2",
        "content": "An algorithm passed by and decided to add xml tags to the text."
      }
      {
        "title": "Chapter 3",
        "content": "A data manager stumbled upon the data, thought it would be relevant for the database users, and so she decided to add it to her database. The end."
      }
  ]
}

This structure (or semi-structure) is just as good as the previous one. That flexibility is one of the reasons why semi-structured data formats and databases are growing in popularity. Because the data structure is included directly in the document, the users do not need to follow a predefined structure and use predefined tags to store their data in the database. This means that, theoretically, every record could have a different structure, which would be fine, but most likely not optimal for an end user trying to make sense of a dataset and extract useful knowledge from it.

Structured data

Structured data and databases are typically tabular (consisting of rows and columns, like an Excel spreadsheet). Each row is a record or entry, and each column is a field (also called features, or variable). The structure is part of the database design (and not each database entry, as in the semi-structured format), and so every new record has to follow that same format. For example, here is the same work of art in a structured format.

Example of structured data
Title Author Section Content
A data story Philippe Mongeon Chapter 1 Once upon a time a data was created by a random internet user and abandoned in a see of unstructured data.
A data story Philippe Mongeon Chapter 2 An algorithm passed by and decided to add xml tags to the text.
A data story Philippe Mongeon Chapter 3 A data manager stumbled upon the data, thought it would be relevant for the database users, and so she decided to add it to include it in her database. The end.

Here, the structure is determined by the columns I chose for my table (title, author, section, and content). But, again, this was an arbitrary choice, and I could have chosen a completely different structure (although the realm of possibilities is somewhat limited by the data and by common sense). The point is that there is rarely an absolute best way of structuring data, and the best structure is the one that best suits the needs of the users. Sometimes the same data may be duplicated and structured differently to suit different users and uses. There are however data structure standards that you should follow when using certain established data models and DBMS, but these are beyond the scope of this course.

The power of structure

The more structured a dataset is, the more easily it will be to use to extract knowledge by an end-user. Unfortunately, data is not always found in easily exploitable structures and will often require some cleaning and processing. There is a well-known saying that data scientists spend 80% of their time cleaning their data. Clean data that fits a clear purpose is actually extremely easy to analyze… the problem is that the purpose isn’t always clear in the end of the user, and data does not always come in the exact format needed for that purpose.

the JSON structure

A JSON file is nothing more than a text file that contains text that follows the JSON syntax. So we can create them with any text editor (e.g. notepad, MS Word, Google Doc). A JSON file must always begin with and end with a curly bracket {}.

Key-value pairs

In JSON, everything is a key-value pair except for objects and arrays for which the key is optional. The key is always between double quotes, and so are values that are strings. Here’s an example of a key-value pair.

"name":"Barry Bonds"

Here “name” is the key, and “Barry Bonds” is the value. This data is the equivalent of an spreadsheet that looks like this:

So a JSON file with only that one key-value pair would look like this:

{ 
  "name":"Barry Bonds"
} 

Types of values in JSON

In JSON, values must be one of the following data types:

  • String: “name” : “bada booms”
  • Number: “number_of_children” : 10
  • Boolean: “sale” : true
  • NULL: “middlename” : null
  • Object: {“name” : “John”, “age” : 30, “city” : “New York”}
  • Array: [“John”, “Anna”, “Peter”]

Creating a JSON object

Here’s an example of a fictional course written as a JSON, which is an example of a JSON object that could be part of a JSON collection.

{
  "code": "INFO6540",
  "semester": "Winter",
  "year": 2023,
  "description": "This is the course description",
  "brightspace_active": true,
  "students": ["Wayne Gretzky","Mario Lemieux"],
  "supplementary_files_url": null
}

And here is another course.

{
  "code": "INFO6270",
  "semester": "Fall",
  "year": 2022,
  "name": "Singing for dummies",
  "description": "This the course description for INFO6270",
  "brightspace_active": false,
  "room": "ROWE1234",
  "students": ["Justin Bieber","Lady Gaga"],
  "supplementary_files_url": "www.info6270_sup.ca"
}

we can see that the document contains strings (code, semester, description), numbers (year), booleans (brightspace_active), and an array (students) that contain objects.

Creating a collection (JSON array)

A collection is essentially a group of objects in an array. For example, if we wanted to store all our courses together into a collection of courses, we could do it in a single JSON file that contains an array called “courses”, like this:

{
  "courses": [
    {
      "code": "INFO6540",
      "semester": "Winter",
      "year": 2023,
      "description": "This is the course description",
      "brightspace_active": true,
      "students": ["Wayne Gretzky","Mario Lemieux"],
      "supplementary_files_url": null
    },
    {
      "code": "INFO6270",
      "semester": "Fall",
      "year": 2022,
      "name": "Singing for dummies",
      "description": "This the course description for INFO6270",
      "brightspace_active": false,
      "room": "ROWE1234",
      "students": ["Justin Bieber","Lady Gaga"],
      "supplementary_files_url": "www.info6270_sup.ca"
    }
  ]
}
TipJSON objects in a collection can have different structures

In an Excel spreadsheet, all rows have the same set of columns in the same order. Considering that a row is the equivalent of a JSON object, it means that in Excel all the objects (rows) are described using the same keys (columns).

Because JSON is a semi-structured format, it does not have this requirement: two objects can have different keys. Here is an example of a collection of two objects (courses) that do not have the same keys:

{
  "courses": [
    {
      "code": "INFO6540",
      "semester": "Winter",
      "year": 2023,
      "description": "This is the course description",
      "location": "Rowe 4321",
      "brightspace_active": true,
      "students": ["Wayne Gretzky","Mario Lemieux"],
      "supplementary_files_url": null
    },
    {
      "offered": "Fall",
      "name": "Singing for dummies",
      "description": "This the course description for INFO6270",
      "building":"Rowe",
      "room": "1234",
      "instructor":"Celine Dion",
      "students": ["Justin Bieber","Lady Gaga"],
    }
  ]
}

Converting a JSON collection to Excel

Here is a JSON collection that we might want to convert to Excel.

{
  "courses": [
    {
      "code": "INFO6540",
      "semester": "Winter",
      "title": "Data Management",
      "year": 2023,
      "description": "This is the course description",
      "room": "Rowe 4321",
      "brightspace_active": true,
      "students": ["Wayne Gretzky","Mario Lemieux"]
    },
    {
      "course_code" "INFO6270"
      "offered": "Fall",
      "name": "Singing for dummies",
      "description": "This the course description for INFO6270",
      "building":"Rowe",
      "room": "1234",
      "instructor":"Celine Dion",
      "students": ["Justin Bieber","Lady Gaga"],
    }
  ]
}

Step 1. Identify all unique keys

In the example above, the keys for the first object are:

  • code
  • semester
  • title
  • year
  • description
  • location
  • brightspace_active
  • students

The keys for the second object are:

  • course_code
  • offered
  • name
  • description
  • building
  • room
  • instructor
  • students

So the unique keys would be:

  • code
  • semester
  • title
  • year
  • description
  • location
  • brightspace_active
  • students
  • course_code
  • offered
  • name
  • building
  • room
  • instructor
  • students

Step 2. Normalize key names

Some of the keys represent the same information but simply have different names. Normalizing the names can reduce the number of total unique keys.

Key of object 1 key of object 2 normalized key name
code course_code code
offered semester semester
title name title

Also, you might have noticed that in the first object, the location contains the building and the room, but in the second object, the building and the room are separate keys. There is no best approach here, but we must choose one.

Key(s) of object 1 Keys of object 2 normalized key(s) name
location building + room location

Step 3. Create a column for each normalized key

  • code
  • semester
  • title
  • year
  • description
  • location
  • brightspace_active
  • instructor
  • students
Code semester title year description location brightspace_active instructor students

Step 4. Create a row for each object

Code semester title year description location brightspace_active instructor students
INFO6540 Winter Data Management 2023 This is the course description Rowe 4321 true Waynte Gretzky; Mario Lemieux
INFO6270 Fall Singing for dummies This is the course description for INFO6270 Rowe 1234 Celine Dion Justin Bieber; Lady Gaga
Note

When an object does not include one of the keys, we simply leave this cell blank in the spreadsheet!

Frequent issues with JSON to Excel Conversion

Here is an example JSON object with an array and a nested object.

{
  "title":"The Mandalorian and Grogu"
  "genre":E["action","comedy"]
  "director": [
                "first_name": "Jon",
                "last_name": "Favreau"
              ]
}

Dealing with arrays

Two options:

Option 1. Creating a different row for each genre

title genre
The Mandalorian and Grogu action
The Mandalorian and Grogu comedy


Option 2. Using a delimitor (comma, or a semi-colon, etc.)

title genre
The Mandalorian and Grogu action; comedy

Dealing with nested objects

Option 1. Combine the keys in a single column

title director
The Mandalorian and Grogu Jon Favreau


Option 2. Create a column for each key

title director_first_name director_last_name
The Mandalorian and Grogu John Favreau


Option 3. Store the JSON directly in Excel

This option is only recommended when the JSON object is complex and option 1 or 2 are not suitable.

title director
The Mandalorian and Grogu {“first_name”:“Jon”, “last_name”:“Favreau”}

complex structures

Some JSON files can have very complex structures with nested objects that content other nested objects, that contain other nested objects, etc. In these scenarios, conversion to Excel might be an equally complex task which will most likely involved dividing the data into multiple spreadsheets. Dealing with these complex JSON structures is beyond the scope of this course.

Additional resources

You can use this online JSON validator and formatter to help you write JSON and check if your syntax is correct.