pythonjsonexport-to-csvjqtinder

How to transform your Tinder data.json into a CSV


Tinder allows you to export your data (https://account.gotinder.com/data), which ends up exporting a data.json file.

How can I transform this nested json into a CSV file I can load into a spreadsheet?

The file looks something like this:

$ cat data.json  |jq .Usage
{
  "app_opens": {
    "2018-06-03": 3,
    "2018-06-04": 10,
    "2018-06-05": 2,
...

With usage for:

messages_sent
matches
messages_received
swipes_passes
swipes_likes
app_opens

A full json with the interesting data would look like:

{
    "Usage": {
        "app_opens": {
            "2018-06-03": 3,
            "2018-06-04": 10,
            "2018-06-05": 2
        },
        "messages_sent": {
            "2018-06-03": 7,
            "2018-06-04": 9,
            "2018-06-05": 0
        },
        "matches": {
            "2018-06-03": 3,
            "2018-06-04": 1,
            "2018-06-05": 7
        },
        "messages_received": {
            "2018-06-03": 30,
            "2018-06-04": 1,
            "2018-06-05": 20
        },
        "swipes_passes": {
            "2018-06-03": 56,
            "2018-06-04": 1,
            "2018-06-05": 8
        },
        "swipes_likes": {
            "2018-06-03": 30,
            "2018-06-04": 4,
            "2018-06-05": 4
        }
    }
}

The output should look like this:

date,messages_sent,matches,messages_received,swipes_passes,swipes_likes,app_opens
2018-06-03,0,2,0,4,10,2
2018-06-04,2,2,1,1,18,6
2018-06-05,35,7,32,1,47,3

Solution

  • This Python code will do the job:

    from __future__ import print_function
    import json
    import itertools
    
    # load json into an object
    with open('data.json') as f:
      d = json.load(f)
    usage = d['Usage']
    
    # get all listed dates
    dates = sorted(set(itertools.chain.from_iterable([[day for day in usage[t]] for t in usage])))
    
    # pivot data into one row per date with multiple columns
    print(','.join(['date']+[t for t in usage]))
    for day in dates:
      print(','.join([day] + [str(usage[t][day]) for t in usage]))
    

    This will transform the usage data in the json file into a csv that will look like:

    date,messages_sent,matches,messages_received,swipes_passes,swipes_likes,app_opens
    2018-06-03,0,2,0,4,10,2
    2018-06-04,2,2,1,1,18,6
    2018-06-05,35,7,32,1,47,3
    2018-06-06,16,1,9,4,32,2