title: YAML to JSON Ruby
body: <p>I have a YAML file that looks like this (with bot names and their parameters):</p>
  
  <pre><code>conf_file:
  
    pipeline_conf_path: /opt/etc/pipeline.conf
    runtime_conf_path: /opt/etc/runtime.conf
  
  
    asn_lookup:
      parameters:
        database: /opt/var/lib/bots/asn_lookup/ipasnteste.dat
      group: "Expert"
      name: "ASN Lookup"
      module: "one module"
      description: "modified by "
  
    modify:
      parameters:
        configuration_path: /opt/var/lib/bots/modify/modify.conf
      group: "Expert"
      name: "Modify"
      module: "one module"
      description: "modified"
  
   filter:
      parameters: 
        filter_action:
        filter_key:
        filter_regex:
        filter_value:
  
      group: "Expert"
      name: "Filter"
      module: "one module"
      description: "modified"
  </code></pre>
  
  <p>And I would like to convert each bot to JSON. For example for the asn-lookup the output should be something like:</p>
  
  <pre><code> "asn-lookup": {
          "parameters": {
              "database": "/opt/var/lib/bots/asn_lookup/ipasnteste.dat"
          },
          "group": "Expert",
          "name": "ASN Lookup",
          "module": "one module",
          "description": "modified by"
      }
  </code></pre>
  
  <p>I already have the following code:</p>
  
  <pre><code>def generate_asn_bot
    config = YAML.load_file('my_conf.yaml')
    asn = config["conf_file"]["asn_lookup"]
    puts JSON.pretty_generate(asn)
  end
  </code></pre>
  
  <p>and it gives the following output:</p>
  
  <pre><code>{
    "parameters": {
      "database": "/opt/intelmq/var/lib/bots/asn_lookup/ipasnteste.dat"
    },
    "group": "Expert",
    "name": "ASN Lookup",
    "module": "intelmq.bots.experts.asn_lookup.expert",
    "description": "modified by mfelix"
  }
  </code></pre>
  
  <p>But it's missing the bot name.
  So I added the following line to the code:</p>
  
  <pre><code>final = asn['name'] = '"asn-lookup"' + ': ' + asn.to_json
  </code></pre>
  
  <p>And use <code>JSON.pretty_generate(final)</code> but it's not working, throwing the error:</p>
  
  <blockquote>
    <p>only generation of JSON objects or arrays allowed (JSON::GeneratorError)</p>
  </blockquote>
  
  <p>What is the best way to convert each bot into JSON and add bot name at the beginning of it?</p>