将解码后的json数组解析为csv

时间:2014-10-08 21:02:14

标签: php arrays json mongodb csv

我有一个包含数百万个这种大小的数组的文件:

{
  "leagues" : [{
      "tier" : "SILVER",
      "entries" : [{
          "playerOrTeamId" : "359",
          "playerOrTeamName" : "TryHard",
          "division" : "II",
          "leaguePoints" : "63",
          "wins" : "65"
        }],
      "id" : "359"
    }],
  "summonerId" : "359",
  "region" : "euw",
  "updatedAt" : "1412122432"
},

例如,最小的数组。有些数组有额外的链接数组,其中包含与主数组相关的额外信息。例如:

{
  "summonerId" : "477",
  "region" : "euw",
  "leagues" : [{
      "tier" : "GOLD",
      "entries" : [{
          "playerOrTeamId" : "477",
          "playerOrTeamName" : "Alucard662545",
          "division" : "V",
          "leaguePoints" : "9",
          "wins" : "128"
        }]
    }, {
      "tier" : "SILVER",
      "entries" : [{
          "playerOrTeamId" : "TEAM-8d6a3640-2da8-11e2-99dc-782bcb4ce61a",
          "playerOrTeamName" : "CAPCOMP BE",
          "division" : "V",
          "leaguePoints" : "0",
          "wins" : "24"
        }]
    }, {
      "tier" : "BRONZE",
      "entries" : [{
          "playerOrTeamId" : "TEAM-8d6a3640-2da8-11e2-99dc-782bcb4ce61a",
          "playerOrTeamName" : "CAPCOMP BE",
          "division" : "I",
          "leaguePoints" : "55",
          "wins" : "8"
        }]
    }],
  "updatedAt" : "1410786559"
},

我一直把头发从脑袋里拔出来,花两天两夜来搞清楚。我有MongoDB存储此信息,当我导出它时,我只能获得解码的JSON数组。我需要这些东西完全CSV格式化。我怎么用CSV来格式化像这样的百万个数组呢?

1 个答案:

答案 0 :(得分:1)

您有两种选择:

mongoexport是一个实用程序,可以生成存储在MongoDB实例中的数据的JSON或CSV导出。用法示例:

mongoexport --db users --collection contacts --csv --fieldFile fields.txt --out /opt/backups/contacts.csv

从集合contacts获取\ r-terminated fields.txt文件中指定的字段,每行一个,并将它们放入/opt/backups/contacts.csv。

以其他方式将JSON读入语言并编写csv。 python中的一个例子如下:

from pymongo import  MongoClient
import csv
client = MongoClient()
db = client['test-database']
collection = db.test_collection
writer = csv.writer('/opt/backups/contacts.csv')
writer.writerow([k for k in collection])
writer writerows([[v for v in c] for c in collection])

......做同样的希望,帮助。