将JSON键值转换为CSV

时间:2015-02-25 16:09:46

标签: ruby json csv

我的目标是读取CSV文件,从该文件的记录中获取每个ID,将每个ID用于Meetup API URL,然后使用JSON响应中的某些值创建新的CSV文件。

这是我到目前为止所拥有的:

require "net/https"
require "uri"
require 'csv'
require 'json'

membersCSV = CSV.foreach('id-members-meetup.csv') do |row|
id = row[1]
uri = URI.parse("https://api.meetup.com/2/members?order=name&member_id=" + id + "&format=json&key=MY_KEY")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
CSV.open("ghmeetup.csv", "w", {:col_sep => ";"}) do |csv|
  JSON.parse(response.body)["other_services"].each do |single|
    csv << [single["twitter"]["identifier"], single["facebook"]["identifier"], single["linkedin"]["identifier"]]
  end
end
end

这是我得到的错误:

/Library/Ruby/Gems/2.0.0/gems/json-1.8.2/lib/json/common.rb:155:in `parse': 757: (JSON::ParserError) '<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>cloudflare-nginx</center>
</body>
</html>
'
    from /Library/Ruby/Gems/2.0.0/gems/json-1.8.2/lib/json/common.rb:155:in `parse'
    from ghmeetup.rb:13:in `block (2 levels) in <main>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1266:in `open'
    from ghmeetup.rb:12:in `block in <main>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1716:in `each'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1120:in `block in foreach'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1266:in `open'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/csv.rb:1119:in `foreach'
    from ghmeetup.rb:6:in `<main>'
你怎么看?

修改

require "uri"
require 'csv'
require 'json'
require 'net/http'

ghCSV = CSV.foreach('id-gh-meetup.csv') do |row|
    id = row[1]
    key="KEY" 
    uri = URI.parse("https://api.meetup.com/2/members?order=name&member_id=#{id}&format=json&key=#{key}")
    Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    request = Net::HTTP::Get.new uri
    response = http.request request
    parseResponse = JSON.parse(response.body)['results'][0]
    p "working"
      CSV.open("ghmeetup.csv", "w") do |csv|
        p "working 2"       
            parseResponse.each do |single|
                p "working 3"
              csv << single
            end
        end
    end
end

如果我只保留JSON.parse(response.body),那么它可以工作但是当我在parseResponse中添加[&#39;结果&#39;] [0]时,我收到此错误:

ghmeetup.rb:15:in `block (2 levels) in <main>': undefined method `[]' for nil:NilClass (NoMethodError)

这是JSON结构,我想定位[results] [0] .other_services.twitter.identifier

{
results: [
 - {
   - other_services: {
      twitter: {
        identifier: "@HugoAmsellem"

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

通过#use_ssl=

为HTTP连接启用HTTPS

此代码使用Ruby 2.2.0在我的系统上获得成功响应:

require 'net/http'  # Not HTTPS
key="..."  # Get your personal API key from Meetup
uri = URI.parse("https://api.meetup.com/2/members?order=name&member_id=1&format=json&key=#{key}")
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
  request = Net::HTTP::Get.new uri
  response = http.request request
  p response.body
end

在早期版本的Ruby中,您需要require 'net/https'才能使用HTTPS。这是no longer true

您可以在系统上试用上面的代码吗?

如果有效,那很好。如果它不起作用,那么您可以简化您的问题代码,例如省略CSV,循环,JSON等。