Skip to content

JSON Unpacked

You'll take a JSON export from an app you use, pull its nested mess apart into proper tables with json_each and json_extract, and query it as if it had been a CSV all along.

About

Most exports now arrive as JSON rather than CSV: streaming history, Google Takeout, a chat archive, a fitness app, a bank's newer download. Beginners open one, see braces nested five deep, and close it. SQLite, DuckDB and Postgres all have JSON functions that turn those braces into rows and columns, and once you've done it once you'll never be put off by an export again. This badge is one real export of your own, flattened into tables you designed, with the lists-inside-records problem solved properly rather than as a comma-joined string. Treat the file as private: it's your data, so post counts and charts rather than rows. What counts is an export you requested from an app, loaded raw and unpacked with SQL rather than hand-edited in a text editor.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
A weekend
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in SQL & Databases to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Request an export from an app you use and open the JSON in a text editor to find its shape. Spotify, Google Takeout, Strava, Discord, Reddit and most banks have one in their settings, and some take days to arrive, so ask early. Find the top-level array, the fields on each record, and any field that is itself a list. Write the shape as an indented outline in a text file. Take a screenshot of the outline and the first record, with any personal values blurred. Photo
  2. 2
    Load the raw file into one table with a single JSON column. In SQLite, one row holding the whole document is fine to start, or one row per record if the file is JSON lines. In DuckDB, SELECT * FROM read_json_auto('file.json') does the whole job in one line, which only counts if you can also say what it made. In Postgres, a jsonb column. Take a screenshot of the table with json_valid() or the engine's type check returning true. Photo
  3. 3
    Flatten the top-level records into a table with real columns and types. json_each() turns an array into rows, and json_extract() or the ->> operator pulls fields out of each one. The shape is CREATE TABLE plays AS SELECT json_extract(value, '$.ts') AS played_at, json_extract(value, '$.ms_played') AS ms FROM raw_docs, json_each(raw_docs.body), where raw_docs is the one-column table from the last step. Convert timestamps to ISO and durations to a number you can SUM while you're at it. Take a screenshot of the CREATE and a COUNT(*) of the result. Photo
  4. 4
    Pull a nested list out into its own table with a foreign key back to the parent. A record with a list inside it (tags, participants, splits, artists) is two tables, not one column. Chain a second json_each on the inner field so you get one row per inner item, carrying the parent's id along. Take a screenshot of the child table and a join back to the parent. Photo
  5. 5
    Answer three questions about yourself that the app's own screens don't. Hours of music per weekday, the longest gap in days between messages with one friend, the month you ran the most. Use a GROUP BY, a join and a date function between them. Take a screenshot of the three queries and their aggregated results. Photo
  6. 6
    Save the whole unpacking as a .sql file that reruns from the raw file. Drop the derived tables, run the file, and get the same counts. Next year's export will have a renamed field, and this file is where you'll fix it in five minutes instead of an evening. Take a screenshot of the file and the matching counts after a rerun. Photo