Ruby:根据多个标准从深度嵌套的JSON结构中提取 [英] Ruby: Extract from deeply nested JSON structure based on multiple criteria
问题描述
我想选择 基于此前 answer ,您只需要在eventNodes上添加一个选择: 如果您想保留国家代码和名称信息id: I want to select any
Based on this previous answer, you just need to add a select on eventNodes : If you want to keep the country code and name information with the id:
这篇关于Ruby:根据多个标准从深度嵌套的JSON结构中提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! marketName =='Moneyline'
的任何 marketId
,但只有那些 countryCode =='US'|| 'GB'
或 eventName.include?('@')
。 ( @
之前和之后的 countryCode
,这对我来说很复杂。 这个是源代码,但是它可能是这样的样本: GBP,
eventTypes=> [
{eventTypeId=> 7522,
eventNodes=> [
{eventId=> 28024331,
event=>
{eventName=>EWE Baskets Oldenburg v PAOK Thessaloniki BC
},
marketNodes=> [
{marketId=>1.128376755 ,
description=>
{marketName=>Moneyline}
},
{marketId=>1.128377853,
description=>
{marketName=>Start Lublin +7.5}
}}}}},
{eventId=> 28023434,
event=> ;
{eventName=>Asseco Gdynia v Start Lublin,
countryCode=>PL,
},
marketNodes=>
[{marketId=>1.128377853,ETC ...
需要'json'
json = File.read('data.json')
hash = JSON.parse(json)
moneyline_market_ids = hash [事件类型]地图{|。类型|
type [eventNodes]。select {| event_node |
['US','GB']。include?(event_node [event] [countryCode])|| event_node [event] [eventName]。include?('@')
} .map {| event |
事件[marketNodes]。select {| market |
market [description] [marketName] =='Moneyline'
} .map {| market |
market [marketId]
}
}
} .flatten
puts moneyline_market_ids.join(',')
#= > 1.128255531,1.128272164,1.128255516,1.128272159,1.128278718,1.128272176,1.128272174,1.128272169,1.128272148,1.128272146,1.128255464,1.128255448,1.128272157,1.128272155,1.128255499,1.128272153,1.128255484,1.128272150,1.128255748,1.128272185,1.128278720,1.128272183,1.128272178,1.128255729,1.128360712, 1.128255371,1.128255433,1.128255418,1.128255403,1.128255387
moneyline_market_ids = hash [eventTypes]。map {| type |
type [eventNodes]。map {| event_node |
[event_node,event_node [event] [countryCode],event_node [event] [eventName]]
} .select {| _,country,event_name |
['US','GB']。include?(country)|| event_name.include?('@')
} .map {| event,country,event_name |
事件[marketNodes]。select {| market |
market [description] [marketName] =='Moneyline'
} .map {| market |
[market [marketId],country,event_name]
}
}
} .flatten(2)
需要'pp'
pp moneyline_market_ids
#=> [[1.128255531,美国,费城@西雅图],
#[1.128272164,美国,阿肯色州@密西西比州],
#[1.128255516,美国,新英格兰@旧金山],
#[1.128272159,US,Indiana @ Michigan],
#[1.128278718,CA,渥太华],
#[1.128272176,US,Arizona State @ Washington],
#[1.128272174,US,Alabama A& M @ Auburn
#...
marketId
of marketName == 'Moneyline'
but only those with countryCode == 'US' || 'GB'
OR eventName.include?(' @ ')
. (space
before and after the @
). I tried different combos of map and select but some nodes don't have countryCode
which complicates things for me. This is the source, but a sample of what it might look like:{"currencyCode"=>"GBP",
"eventTypes"=>[
{"eventTypeId"=>7522,
"eventNodes"=>[
{"eventId"=>28024331,
"event"=>
{"eventName"=>"EWE Baskets Oldenburg v PAOK Thessaloniki BC"
},
"marketNodes"=>[
{"marketId"=>"1.128376755",
"description"=>
{"marketName"=>"Moneyline"}
},
{"marketId"=>"1.128377853",
"description"=>
{"marketName"=>"Start Lublin +7.5"}
}}}]},
{"eventId"=>28023434,
"event"=>
{"eventName"=>"Asseco Gdynia v Start Lublin",
"countryCode"=>"PL",
},
"marketNodes"=>
[{"marketId"=>"1.128377853", ETC...
require 'json'
json = File.read('data.json')
hash = JSON.parse(json)
moneyline_market_ids = hash["eventTypes"].map{|type|
type["eventNodes"].select{|event_node|
['US', 'GB'].include?(event_node["event"]["countryCode"]) || event_node["event"]["eventName"].include?(' @ ')
}.map{|event|
event["marketNodes"].select{|market|
market["description"]["marketName"] == 'Moneyline'
}.map{|market|
market["marketId"]
}
}
}.flatten
puts moneyline_market_ids.join(', ')
#=> 1.128255531, 1.128272164, 1.128255516, 1.128272159, 1.128278718, 1.128272176, 1.128272174, 1.128272169, 1.128272148, 1.128272146, 1.128255464, 1.128255448, 1.128272157, 1.128272155, 1.128255499, 1.128272153, 1.128255484, 1.128272150, 1.128255748, 1.128272185, 1.128278720, 1.128272183, 1.128272178, 1.128255729, 1.128360712, 1.128255371, 1.128255433, 1.128255418, 1.128255403, 1.128255387
moneyline_market_ids = hash["eventTypes"].map{|type|
type["eventNodes"].map{|event_node|
[event_node, event_node["event"]["countryCode"], event_node["event"]["eventName"]]
}.select{|_, country, event_name|
['US', 'GB'].include?(country) || event_name.include?(' @ ')
}.map{|event, country, event_name|
event["marketNodes"].select{|market|
market["description"]["marketName"] == 'Moneyline'
}.map{|market|
[market["marketId"],country,event_name]
}
}
}.flatten(2)
require 'pp'
pp moneyline_market_ids
#=> [["1.128255531", "US", "Philadelphia @ Seattle"],
# ["1.128272164", "US", "Arkansas @ Mississippi State"],
# ["1.128255516", "US", "New England @ San Francisco"],
# ["1.128272159", "US", "Indiana @ Michigan"],
# ["1.128278718", "CA", "Edmonton @ Ottawa"],
# ["1.128272176", "US", "Arizona State @ Washington"],
# ["1.128272174", "US", "Alabama A&M @ Auburn"],
# ...