如何将 Tinder data.json 转换为 CSV

How to transform your Tinder data.json into a CSV

Tinder 允许您导出数据 (https://account.gotinder.com/data),最终导出 data.json 文件。

如何将这个嵌套的 json 转换为可以加载到电子表格中的 CSV 文件?

文件看起来像这样:

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

用于:

messages_sent
matches
messages_received
swipes_passes
swipes_likes
app_opens

包含有趣数据的完整 json 如下所示:

{
    "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
        }
    }
}

输出应如下所示:

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

这段 Python 代码可以完成工作:

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]))

这会将 json 文件中的使用数据转换为如下所示的 csv:

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

为了给约会对象留下深刻印象,您显然需要比 Python 更古怪的东西。 jq 是一个不错的选择,因为输入格式是 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))[]

运行 像这样:

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

这输出:

"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

...可以作为电子表格打开。

这是一个易于阅读且功能强大的 jq 程序,可以完成这项工作:

["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

它在几个意义上是 "robust" -- 输入对象中键的顺序并不重要,以日期作为键的对象不需要具有相同的键,它们不需要在相同的顺序,或者实际上是按时间顺序。在所有具有相同或更高鲁棒性的程序中,该程序也是高效的。