【问题标题】:Combine JSON objects in Ruby, and output to CSV在 Ruby 中组合 JSON 对象,并输出到 CSV
【发布时间】:2016-05-10 17:31:15
【问题描述】:

假设我有两个 JSON 对象(称它们为“网站”和“链接”)。我需要将两个对象都放在一个 CSV 中(最好是在单独的列中)。

我正在处理这样的事情:

File.open("file.json", "w") do |f|
   combined = [websites, links]
   f.write(JSON.pretty_generate(combined))
end

然后我使用 Ruby gem json2csv 将此文件转换为 CSV。但是当我这样做时,我收到以下错误:

error: undefined method 'keys' for #<Array:0x007fea8a8e33f8>

我不知道出了什么问题。当我查看 file.json 时,它的结构似乎是这样的:[{websites}, {links}]。以我对 JSON 的有限了解,我认为这是对的,但我很容易出错。

另外,我知道这不会让我在 CSV 中分离列。如果有人对这部分有答案,主要的奖励积分。

编辑:下面包含 JSON 示例;错误消息在我的小修后改变了。

网站:

{
  "uri": "https://v1/websites",
  "id": 28235674,
  "background": null,
  "createdDate": 1399585684000,
  "lastActivityDate": 1430682494000,
  "lastCommunicationDate": 1430682494000,
  "lastNonCommunicationChronicleDate": 1430330886000,
  "lastModifiedDate": 1449263116000,
  "lastViewedDate": 1421429034000,
  "preferredContactType": null,
  "rss": "",
  "emailAddresses": [
    {
      "email": "",
      "type": "Work"
    },
    {
      "email": "",
      "type": "Work"
    },
    {
      "email": "",
      "type": "Work"
    },
    {
      "email": "not found",
      "type": "Work"
    }
  ],
  "phoneNumbers": [

  ],
  "streetAddresses": [

  ],
  "socialNetworks": [
    {
      "profileUrl": "http://twitter.com",
      "name": "Twitter"
    },
    {
      "profileUrl": "http://www.facebook.com",
      "name": "Facebook"
    },
    {
      "profileUrl": "http://plus.google.com",
      "name": "GooglePlus"
    },
    {
      "profileUrl": "http://www.linkedin.com",
      "name": "LinkedIn"
    },
{
  "profileUrl": "http://twitter.com",
  "name": "Twitter"
}
  ],
  "contactUrls": [

  ],
  "tags": [
    "tag1",
    "tag2"
  ],
  "mostRecentActivity": "https://v1/history",
  "mostRecentChronicle": "https://v1/history",
  "mostRecentCommunication": "https://v1/history",
  "mostRecentNonCommunicationChronicle": "https://v1/history",
  "projectStates": "https://v1/websites",
  "history": "https://v1/history",
  "customFieldValues": [

  ],
  "name": "",
  "primaryDomain": "",
  "domains": [
    ""
  ],
  "associatedPeople": "https://v1/people",
  "payments": "https://payments",
  "links": "https://v1/links",
  "type": "https://v1/websites"
}

链接:

{
  "uri": "https://v1/links/custom_fields",
  "id": 15529329,
  "value": "Name",
  "backgroundColor": null,
  "customField": "https://v1/links/custom_fields"
}

组合输出:

[
  {
    "uri": "https://v1/websites",
    "id": 28235674,
    "background": null,
    "createdDate": 1399585684000,
    "lastActivityDate": 1430682494000,
    "lastCommunicationDate": 1430682494000,
    "lastNonCommunicationChronicleDate": 1430330886000,
    "lastModifiedDate": 1449263116000,
    "lastViewedDate": 1421429034000,
    "preferredContactType": null,
    "rss": "",
    "emailAddresses": [
      {
        "email": "",
        "type": "Work"
      },
      {
        "email": "",
        "type": "Work"
      },
      {
        "email": "",
        "type": "Work"
      },
      {
        "email": "not found",
        "type": "Work"
      }
    ],
    "phoneNumbers": [

    ],
    "streetAddresses": [

    ],
    "socialNetworks": [
      {
        "profileUrl": "http://twitter.com/",
        "name": "Twitter"
      },
      {
        "profileUrl": "http://www.facebook.com",
        "name": "Facebook"
      },
      {
        "profileUrl": "http://plus.google.com",
        "name": "GooglePlus"
      },
      {
        "profileUrl": "http://www.linkedin.com/",
        "name": "LinkedIn"
      },
      {
        "profileUrl": "http://twitter.com/",
        "name": "Twitter"
      }
    ],
    "contactUrls": [

    ],
    "tags": [
      "tag1",
      "tag2"
    ],
    "mostRecentActivity": "https://v1/history/",
    "mostRecentChronicle": "https://v1/history/",
    "mostRecentCommunication": "https://v1/history/",
    "mostRecentNonCommunicationChronicle": "https://v1/history/",
    "projectStates": "https://v1/websites/",
    "history": "https://v1/history",
    "customFieldValues": [

    ],
    "name": "",
    "primaryDomain": "",
    "domains": [
      ""
    ],
    "associatedPeople": "https://v1/people",
    "links": "https://v1/links",
    "type": "https://v1/websites"
  },
  {
    "uri": "https://links/custom_fields",
    "id": 15529329,
    "value": "Name",
    "backgroundColor": null,
    "customField": "https://links/custom_fields"
  }
]

【问题讨论】:

  • 我们需要查看实际的 JSON。如果需要,请对 URL 进行打乱,但请保持格式。
  • 什么是“JSON 对象连接”?请编辑您的问题以包含错误发生位置周围的实际 JSON 数据和实际错误消息。
  • 分享websiteslinks JSON 示例

标签: ruby json csv


【解决方案1】:

JSON.pretty_generate() 需要一个哈希值,而您正在传递一个包含 2 个哈希值的数组。从:combined.map { |c| f.write(JSON.pretty_generate(c)) } 开始,然后将它们映射到您的 CSV 中应该就像遵循 CSV 文档一样简单。

【讨论】:

  • 嗯,这仍然无法使用 json2csv,但如果您能提供您想到的替代方案的代码,我将不胜感激。现在的输出格式如下:{websites}{links}(两个对象之间没有逗号或任何内容)。对吗?
猜你喜欢
  • 1970-01-01
  • 2011-06-16
  • 2021-03-03
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多