Accessing JSON objects in Ruby -
i have json file looks kind of this:
{ "results": [ { "lookup": null, "result": { "paths": [ { "domain": "value1.ltd", "url": "", "text1": "", "modules": [ { "name": "value", "tag": "value", "firstdetected": "1111111111", "lastdetected": "11111111111" }, { "name": "value", "tag": "value", "firstdetected": "111111111111", "lastdetected": "11111111111111" } ] } ] } } ] } how print domain , access module.names in ruby , print module.names console:
#!/usr/bin/ruby require 'rubygems' require 'json' require 'pp' json = file.read('input.json') and 1 know of resources ruby , json new it?
json.parse takes json string , return hash can manipulated other hash.
#!/usr/bin/ruby require 'rubygems' require 'json' require 'pp' # symbolize keys makes hash easier work data = json.parse(file.read('input.json'), symbolize_keys: true) # loop through :results if there data[:results].each |r| # loop through [:result][:paths] if there r[:result][:paths].each |path| # path refers current item path[:modules].each |module| # module refers current item puts module[:name] end if path[:modules].any? end if r[:result][:paths].any? end if data[:results].any?
Comments
Post a Comment