1

I have a JSON file composed of bot names and their parameters and configurations and it looks like this:

{
    "csv-collector": {
        "parameters": {
            "delete_file": false,
            "feed": "FileCollector",
            "provider": "ola",
            "path": "/tmp/",
            "postfix": ".csv",
            "rate_limit": 300
        },
        "group": "Collector",
        "name": "Fileinput",
        "module": "abc",
        "description": "Fileinput collector fetches data from a file."
    },
    "csv-parser": {
        "parameters": {
            "columns": "classification.type,destination.ip",
            "default_url_protocol": "http://",
            "delimiter": ",",
            "skip_header": true,
            "type": "c&c"
        },
        "group": "Parser",
        "name": "Generic CSV",
        "module": "efg",
        "description": "Generic CSV Parser is a generic bot"
    }
}

And I would like to parse it to a Ruby Hash with the bot names "csv-collector" and "csv-parser" as the keys and the rest of the content as the values. Something like this:

my_hash = {"csv-collector" => {"parameters": {
                "delete_file": false,
                "feed": "FileCollector",
                "provider": "ola",
                "path": "/tmp/",
                "postfix": ".csv",
                "rate_limit": 300
            },
            "group": "Collector",
            "name": "Fileinput",
            "module": "abc",
            "description": "Fileinput collector fetches data from a file."
            }
         }

I have several bots so this must be valid for the others as well.

I already have the following code:

require "json"

def read_file
  temp = Hash.new
  file = JSON.parse(File.read('mybotsfile.conf'))
  file.each { |bot| temp << bot}
  puts temp
end

But is giving me the following error:

`undefined method `<<' for {}:Hash (NoMethodErr`or)

I'm new to Ruby and I don't know very well how to parse the JSON file to a Ruby Hash

1 Answer 1

7
require 'json'
file = File.read('your-json-file.json')
result_hash = JSON.parse(file)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.