Ruby JSON解析“子键”?

时间:2016-11-25 17:00:37

标签: json ruby parsing

我正在尝试解析我已经解析了一段时间的现有JSON的附加密钥,但是最近我们添加了一个新的密钥“metric_details”,它分隔了各个区域,其中包含2个密钥以及“performance_score” ,和“compliance_score”。现在,我已经能够毫无问题地解析这个问题,但无法弄清楚我的生活如何拉出metric_details并将分数导出到我的RDS中。

解析代码:

   states = %w(CA TX)
results_warehouses = states
creds = User_creds

data_to_write = []
results_warehouses.each do |code|

  dashboard = "https://sample.website.com/states/scorecard.json?state_id=#{code}"
  puts "Pulling from #{dashboard}"

  begin
    data  =  HTTParty.get(dashboard, :basic_auth=> {:username => creds.public.text, :password => creds.private.text})
  rescue
    puts "Could not fetch data for #{code}. Skipping..."
    next
  end

  types = %w(pack ship)
  types.each do |type|

    metric = data[type]
    next if metric.nil?
    metric.sort.each do |key|
      state_id                = key['state_id']
      raw_date                = key['date'].to_date
      # Can't figure how to parse this metric_details          = key['metric_details']
      performance_score       = key['performance_score']
      audit_compliance_score  = key['compliance_score']

      data_to_write << "('#{state_id.to_s}',
                         '#{raw_date.to_s}',
                         '#{type.to_s}',
                         '#{metric_details.to_s}',
                         '#{performance_score.to_s}',
                         '#{compliance_score.to_s}')"
      puts data_to_write
    end
  end

  unless data_to_write.empty?
    puts 'Inserting data'
    db_conn   = DatabaseConnect.new
    db_client = db_conn.connect_mysql
    ins = "INSERT IGNORE INTO `data`.`scoring` VALUES #{data_to_write.join(',')}"
    db_client.query(ins)
  end
  db_conn.disconnect
  puts 'Completed.'
end

puts 'All done!'

而且,在我尝试获取单个指标详细信息之前,这是我正在拆分的JSON,没有任何问题

"pack":[
{
"state_id":"CA",
"total_score":90,
"compliance_score":100,
"performance_score":80,
"date":"2016-11-24T00:00:00.000-08:00",
"metric_details":{
"Damages":{
"performance_score":91,
"compliance_score":100
},
"Error Indicators":{
"performance_score":100,
"compliance_score":"N/A"
},
"Unverified":{
"performance_score":75,
"compliance_score":100
},
"Pack Miss":{
"performance_score":54,
"ompliance_score":100
}
}
}
],

我的表结构相当简单:

基本上想要格式化:

CA,2016-11-25,pack,Damages,75,100

state_id,raw_date,type,metric_details,performance_score,compliance_score

感谢您的帮助!

更新

states = %w(CA TX)
results_warehouses = states
creds = User_creds

data_to_write = []
results_warehouses.each do |code|

  dashboard = "https://sample.website.com/states/scorecard.json?state_id=#{code}"
  puts "Pulling from #{dashboard}"

  begin
    data  =  HTTParty.get(dashboard, :basic_auth=> {:username => creds.public.text, :password => creds.private.text})
  rescue
    puts "Could not fetch data for #{code}. Skipping..."
    next
  end

  types = %w(pack ship)
  types.each do |type|

    metric = data[type]
    next if metric.nil?
    metric.sort.each do |key|
      state_id                = key['state)ud']
      raw_date                = key['date'].to_date
      hash                    = key['metric_details']
      damages                 = hash['Damages']
      performance_score       = damages['performance_score']
      audit_compliance_score  = damages['compliance_score']

      data_to_write << "('#{state_id.to_s}',
                         '#{raw_date.to_s}',
                         '#{type.to_s}',
                         '#{metric_details.to_s}',
                         '#{performance_score.to_s}',
                         '#{audit_compliance_score.to_s}')"
      puts data_to_write
    end
  end

  unless data_to_write.empty?
    puts 'Inserting data'
    db_conn   = DatabaseConnect.new
    db_client = db_conn.connect_mysql
    ins = "INSERT IGNORE INTO `data`.`scoring` VALUES #{data_to_write.join(',')}"
    db_client.query(ins)
  end
  db_conn.disconnect
  puts 'Completed.'
end

puts 'All done!'

1 个答案:

答案 0 :(得分:2)

key['metric_details']返回哈希:

{
  "Damages":{
    "performance_score":91,
    "compliance_score":100
  },
  "Error Indicators": {
    "performance_score":100,
    "compliance_score":"N/A"
  },
  ...
  ...
}

所以,你需要在那个哈希中查找信息:

hash = key['metric_details']
damages = hash['Damages']
performance_score = damages['performance_score']
compliance_score = damgages['compliance_score']

以下是一个完整的例子:

require 'json'

data = %Q{
{"pack":[
{
"state_id": "CA",
"total_score":90,
"compliance_score":100,
"performance_score":80,
"date":"2016-11-24T00:00:00.000-08:00",
"metric_details":{
"Damages":{
"performance_score":91,
"compliance_score":100
},
"Error Indicators":{
"performance_score":100,
"compliance_score":"N/A"
},
"Unverified":{
"performance_score":75,
"compliance_score":100
},
"Pack Miss":{
"performance_score":54,
"ompliance_score":100
}
}
}
]

}
}

data = JSON.parse data
data_to_write = ""

types = %w(pack ship)
types.each do |type|
  metric = data[type]
  next if metric.nil?

  metric.sort.each do |hash|
    state_id                = hash['state_id']
    raw_date                = hash['date']
    # Can't figure how to parse this metric_details          = key['metric_details']
    #performance_score       = key['performance_score']
    #audit_compliance_score  = key['compliance_score']
    metric_details = hash['metric_details']
    damages_hash = metric_details['Damages']
    performance_score = damages_hash['performance_score']
    compliance_score = damages_hash['compliance_score']

    data_to_write << "('#{state_id}',
                       '#{raw_date}',
                       '#{type}',
                       '#{performance_score}',
                       '#{compliance_score}')"
    puts data_to_write
  end
end

--output:--
('CA',
                       '2016-11-24T00:00:00.000-08:00',
                       'pack',
                       '91',
                       '100')