Кажется, это должно быть довольно просто, но я в тупике. Используя Rails, я генерирую хеш с JSON HTTP Request. Я использую JSON.parse, затем перебираю ответ, используя цикл «each» (response.each do |river| do ... end
). У меня вопрос, как я могу получить элемент siteName
только один раз? Я возвращаю дубликаты данных с неполными результатами. Элемент siteName
указан дважды в JSON (см. Пример ниже). Другой подход, не уверен, что он лучший, состоит в удалении всего хеша в массиве с условным поиском flow => 'n/a'
. Имеет ли это смысл? Какова лучшая практика \ решение в этой ситуации? Любая помощь приветствуется.
Вот JSON
, который я анализирую: https://waterservices.usgs.gov/nwis/iv/?format=json&indent=on&sites=09358000,09359020,09359500,09361500,09362520,09363500¶meterCd=00060,00010&siteStatus=active
Возвращается хэш.
[{"siteName"=>"ANIMAS RIVER AT SILVERTON, CO.", "degree"=>"0.4", "flow"=>"n/a", "siteCode"=>"09358000"},
{"siteName"=>"ANIMAS RIVER AT SILVERTON, CO.", "degree"=>"0.4", "flow"=>"25.5", "siteCode"=>"09358000"},
{"siteName"=>"ANIMAS RIVER BELOW SILVERTON, CO", "degree"=>"0.5", "flow"=>"n/a", "siteCode"=>"09359020"},
{"siteName"=>"ANIMAS RIVER BELOW SILVERTON, CO", "degree"=>"0.5", "flow"=>"65.1", "siteCode"=>"09359020"},
{"siteName"=>"ANIMAS RIVER AT TALL TIMBER RESORT ABOVE TACOMA,CO", "degree"=>"0.5", "flow"=>"100", "siteCode"=>"09359500"}, {"siteName"=>"ANIMAS RIVER AT DURANGO, CO", "degree"=>"5.5", "flow"=>"n/a", "siteCode"=>"09361500"},
{"siteName"=>"ANIMAS RIVER AT DURANGO, CO", "degree"=>"5.5", "flow"=>"241", "siteCode"=>"09361500"}, {"siteName"=>"ANIMAS RIVER BLW DURANGO PUMP PLANT NR DURANGO, CO", "degree"=>"5.5", "flow"=>"255", "siteCode"=>"09362520"}, {"siteName"=>"ANIMAS RIVER NEAR CEDAR HILL, NM", "degree"=>"4.2", "flow"=>"n/a", "siteCode"=>"09363500"},
{"siteName"=>"ANIMAS RIVER NEAR CEDAR HILL, NM", "degree"=>"4.2", "flow"=>"284", "siteCode"=>"09363500"}]
Желаемый вывод:
[{"siteName"=>"ANIMAS RIVER AT SILVERTON, CO.", "degree"=>"0.4", "flow"=>"25.5", "siteCode"=>"09358000"},
{"siteName"=>"ANIMAS RIVER BELOW SILVERTON, CO", "degree"=>"0.5", "flow"=>"65.1", "siteCode"=>"09359020"},
{"siteName"=>"ANIMAS RIVER AT TALL TIMBER RESORT ABOVE TACOMA,CO", "degree"=>"0.5", "flow"=>"100", "siteCode"=>"09359500"},
{"siteName"=>"ANIMAS RIVER AT DURANGO, CO", "degree"=>"5.5", "flow"=>"241", "siteCode"=>"09361500"},
{"siteName"=>"ANIMAS RIVER BLW DURANGO PUMP PLANT NR DURANGO, CO", "degree"=>"5.5", "flow"=>"255", "siteCode"=>"09362520"},
{"siteName"=>"ANIMAS RIVER NEAR CEDAR HILL, NM", "degree"=>"4.2", "flow"=>"284", "siteCode"=>"09363500"}]
Код проблемы:
def process_river_telemtry(results)
temp = results.dig("value","timeSeries")
@river_telemetry = []
temp.each do |rivers|
siteName = {'siteName' => rivers.dig("sourceInfo", "siteName")}
siteCode = {'siteCode' => rivers.dig("sourceInfo", "siteCode", 0, "value")}
rivers['variable']['variableCode'].each do |vc|
variableCode = vc['value']
if variableCode == ENV['WATERDATA_PARAM_CODE_TEMP']
@degree = {"degree" => rivers.dig("values", 0, "value", 0, "value")}
end
if rivers['variable']['variableName'] == "Streamflow, ft³/s"
@flow = {"flow" => rivers.dig("values", 0 ,"value", 0, "value")}
else
@flow = {"flow" => "n/a"}
end
end
build = siteName.merge(@degree).merge(@flow).merge(siteCode)
@river_telemetry << build
end
return @river_telemetry
end