ruby:从嵌套的json中提取字段

时间:2014-02-12 18:54:10

标签: ruby json data-extraction

我正在努力教自己红宝石并解决工作中的问题。我的最终目标是从API中提取JSON响应中的许多字段中的三个,操作并转储到CSV以进行执行报告。

JSON的结构是:

{
  "status": 200,
  "data": {
    "total": 251,
    "alerts": [
      {
        "dataPoint": "x",
        "ackedBy": "x",
        "dataSourceInstance": "x",
        "dataSource": "x",
        "host": "x",
        "endOn": 0,
        "ackedOn": 1385085190,
        "dataSourceInstanceId": 588384,
        "hostId": 935,
        "type": "alert",
        "dataSourceId": 694,
        "ackedOnLocal": "2013-11-21 17:53:10 PST",
        "id": 6170384,
        "startOn": 1385084917,
        "thresholds": "!= 1 1 1",
        "endOnLocal": "",
        "level": "error",
        "ackComment": "x",
        "value": "No Data",
        "hostDataSourceId": 211986,
        "acked": true,
        "hostGroups": [{
          "alertEnable": true,
          "createdOn": 1362084592,
          "id": 21,
          "parentId": 78,
          "description": "",
          "appliesTo": "",
          "name": "2600hz",
          "fullPath": "x"
        }],
        "startOnLocal": "2013-11-21 17:48:37 PST"
      },

具体而言,我想提取dataPointstartOnackedOn

我想我需要首先提取总价值,所以我知道我有多少警报。这将帮助我遍历警报实例。

*url = "https://xyz"
uri = URI(url)
http = Net::HTTP.new(uri.host, 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Get.new(uri.request_uri)
response = http.request(req)

# Make sure we had a proper web response
if response.code == '200' then

# Set up that the total is two levels deep in the json
path = JsonPath.new('$.total')

# Extract out total field into variable
totalAlerts = path.on(response)

# Print hash to confirm value
pp totalAlerts

end*

我被困在试图提取总数。输出没有显示任何内容:

sudo ruby alerts.rb
[]

有更好的方法吗?

2 个答案:

答案 0 :(得分:12)

试试这个:

# just for demonstration, you would probably do json = JSON.parse(response)
json = {
  "status": 200,
  "data": {
    "total": 251,
    "alerts": [
      {
        "dataPoint": "x",
        "ackedBy": "x",
        ...

仅限总数:

total = json['data']['total']

对于您询问的其他值:

json['data']['alerts'].each do |alerts|
  # do whatever you want with these...
  alerts['dataPoint']
  alerts['startOn']
  alerts['ackedOn']

现在的问题是,你想对结果做些什么?你想将它们收集到一个新的哈希? json['data']['alerts']是一个数组,因此您必须决定如何收集它们。你可以这样做:

collected_alerts = { 'dataPoints' => [], 'startOns' => [], 'ackedOns' => [] }
json['data']['alerts'].each do |alerts|
  collected_alerts['dataPoints'] << alerts['dataPoint']
  collected_alerts['startOns'] << alerts['startOn']
  collected_alerts['ackedOns'] << alerts['ackedOn']
end

现在,您可以在collected_alerts

中获取所有这些值

答案 1 :(得分:0)

就我而言,我必须将 Logstash 事件转换为 JSON 数组才能从其中一个对象中获取值,所以对我来说是在 Ruby 过滤器上写入这些行:

sources_arr = event.get('[message][body][sources]')
sources_json = JSON.parse(sources_arr.to_json)
event.set('my_source_uri', sources_json[0]['uri'])

注意 JSON.parse(<array>.to_json)