0

I'm trying to parse an array of hashes, grab a value from a specific key, and output to json with one predefined key for each value - and I'm stuck.

Array:

[{:object_id=>"jon-59",
  :name=>"jon-59-gw (8db8fcae-055a-4b35-9f8f-739b68c0bd5d)",
  :client_handle=>nil,
  :extended_attributes=>nil,
  :appliances_summary=>
   {:vm_version=>"5.5.3",
    :vm_build_info=>"5.5.3-2135647"},
  :hypervisor_assist=>false,
  :allowed_actions=>
   {:string=>
     ["Change Log Level",
      "Edit Dns",
      "Edit Syslog"]},
  :edge_assist_id=>"0"},
 {:object_id=>"jon-60",
  :name=>"jon-60-gw (d63ddc45-gd3c-40c3-9046-e7afa996934a)",
  :client_handle=>nil,
  :extended_attributes=>nil,
  :appliances_summary=>
   {:vm_version=>"5.5.3",
    :vm_build_info=>"5.5.3-2168697"},
  :hypervisor_assist=>false,
  :allowed_actions=>
   {:string=>
     ["Change Log Level",
      "Edit Dns",
      "Edit Syslog"]},
  :edge_assist_id=>"0"}]

Desired Output

{
  "data":[
    { "{#JONNAME}":"jon-59-gw" },
    { "{#JONNAME}":"jon-60-gw"},
  ]
}

Where I'm at:

def jon_discover
  jon_summary.sort_by { |jon| jon[:object_id] }.each do |jon|
    name = jon[:name].slice!(/\A\S*/)
    my_hash = {'{#JONNAME}' => name}
    puts JSON.generate(my_hash)
  end
end

The above returns:

{ "{#JONNAME}":"jon-59-gw" }
{ "{#JONNAME}":"jon-60-gw" }
But I don't know where to take it from here, or if I'm on the right track. How can I get this into the desired output?

Thanks, cheers!

0

1 Answer 1

3

This is too complex:

my_hash = {"{#JONNAME}" => "#{name}"}

Keep it simple:

my_hash = {JONNAME => name}

the data is for Zabbix low level discovery

Then use single-quotes instead of double quotes for the key and use the bare name for the value:

my_hash = {'{#JONNAME}' => name}

so it's more apparent that {# is not a typo.


Instead of:

"jon-60-gw (d63ddc45-gd3c-40c3-9046-e7afa996934a)".slice!(/\A\S*/) # => "jon-60-gw"

Use:

"jon-60-gw (d63ddc45-gd3c-40c3-9046-e7afa996934a)".split.first # => "jon-60-gw"

Putting it all together:

require 'json'

ary = [
  {
    :object_id => "jon-59",
    :name => "jon-59-gw (8db8fcae-055a-4b35-9f8f-739b68c0bd5d)",
    :client_handle => nil,
    :extended_attributes => nil,
    :appliances_summary => 
    {
      :vm_version => "5.5.3",
      :vm_build_info => "5.5.3-2135647"
    },
    :hypervisor_assist => false,
    :allowed_actions =>  {
      :string =>  ["Change Log Level", "Edit Dns", "Edit Syslog"]
    },
    :edge_assist_id => "0"
  },
  {
    :object_id => "jon-60",
    :name => "jon-60-gw (d63ddc45-gd3c-40c3-9046-e7afa996934a)",
    :client_handle => nil,
    :extended_attributes => nil,
    :appliances_summary =>  {
      :vm_version => "5.5.3",
      :vm_build_info => "5.5.3-2168697"
    },
    :hypervisor_assist => false,
    :allowed_actions =>  {
      :string =>  ["Change Log Level", "Edit Dns", "Edit Syslog"]
    },
    :edge_assist_id => "0"
  }
]

Here's how to walk through the data:

data = ary.map{ |hash|
  {
    '{#JONNAME}' => hash[:name].split.first
  }
}

Here's how to generate the JSON:

puts JSON[{'data' => data}]
# >> {"data":[{"{#JONNAME}":"jon-59-gw"},{"{#JONNAME}":"jon-60-gw"}]}

If you need it sorted:

puts JSON[{'data' => data.sort_by{ |s| s['{#JONNAME}'] }}]
# >> {"data":[{"{#JONNAME}":"jon-59-gw"},{"{#JONNAME}":"jon-60-gw"}]}
Sign up to request clarification or add additional context in comments.

3 Comments

No, the data is for Zabbix low level discovery, and {#JONNAME} is a macro.
That's awesome, seems so simple now. And thanks for the additional tips!
The biggest thing to learn when programming is recognizing patterns in problems. Then it's easy to grab little pieces of our toolbox that we know do certain things, and apply them correctly. It comes from experience.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.