4

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
peak
  • 105,803
  • 17
  • 152
  • 177
Felipe Hoffa
  • 54,922
  • 16
  • 151
  • 325
  • I added a sample of the data.json file, thanks for asking – Felipe Hoffa Aug 27 '19 at 01:40
  • I'm posting this code because I wish someone already had. It would have saved me a lot of time. Stack Overflow has a policy encouraging this https://stackoverflow.com/help/self-answer. This will be useful for anyone exporting their data from tinder. – Felipe Hoffa Aug 27 '19 at 01:48
  • I tried jq for myself before python. After searching for solutions on stack overflow, decided to do python. – Felipe Hoffa Aug 27 '19 at 01:56
  • 1
    @hek2mgl - I added a full json example - I would love to know how to solve this with `jq` – Felipe Hoffa Aug 27 '19 at 21:31
  • 1
    Cool, thanks. Now the problem is reproducible / answerable by everyone. I think this is essential, also in self-answered threads. Besides that there is really no problem in posting a self-answered question. Added jq program. – hek2mgl Aug 28 '19 at 02:49

3 Answers3

3

To impress your date you obviously need something more hackish than Python. jq is a good choice since the input format is json:

tndr2csv

#!/usr/bin/jq -rf
.Usage as $u|$u|keys as $k|
(["date"]+$k|@csv),
(.[$k[0]]|keys|map(. as $d|[.]+($k|map($u[.][$d]))|@csv))[]

Run it like this:

$ chmod +x tndr2csv
$ ./tndr2csv data.json

This outputs:

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

... which can be opened as a spreadsheet.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thanks! I'll probably keep using the Python solution (for readability), and I'm glad people can now choose between python and jq. As for the downvotes on the perfectly usable Python answer, I guess those downvoters have bigger issues to deal with in their life. Hopefully the low score doesn't confuse people looking for working answers – Felipe Hoffa Aug 28 '19 at 19:53
1

Here is an easy-to-read and robust jq program that does the job:

["date", "messages_sent", "matches", "messages_received", "swipes_passes", "swipes_likes", "app_opens"] as $keys
| .Usage 
| ([.[] | keys_unsorted] | add | unique) as $dates
| $keys,
  ($dates[] as $d | [$d] + [ .[$keys[1:][]][$d] ])
|  @csv

It is "robust" in several senses -- the ordering of the keys within the input objects is unimportant, and the objects with dates as keys need not have the same keys, and they need not be in the same order, or indeed in chronological order. Amongst all programs which have the same or more robustness, this program is also efficient.

peak
  • 105,803
  • 17
  • 152
  • 177
0

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
Felipe Hoffa
  • 54,922
  • 16
  • 151
  • 325