gpt4 book ai didi

json - YAML 到 JSON Ruby

转载 作者:数据小太阳 更新时间:2023-10-29 07:27:57 26 4
gpt4 key购买 nike

我有一个如下所示的 YAML 文件(带有机器人名称及其参数):

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"

我想将每个机器人转换为 JSON。例如,对于 asn-lookup,输出应该是这样的:

 "asn-lookup": {
"parameters": {
"database": "/opt/var/lib/bots/asn_lookup/ipasnteste.dat"
},
"group": "Expert",
"name": "ASN Lookup",
"module": "one module",
"description": "modified by"
}

我已经有以下代码:

def generate_asn_bot
config = YAML.load_file('my_conf.yaml')
asn = config["conf_file"]["asn_lookup"]
puts JSON.pretty_generate(asn)
end

它给出了以下输出:

{
"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"
}

但它缺少机器人名称。所以我在代码中添加了以下行:

final = asn['name'] = '"asn-lookup"' + ': ' + asn.to_json

并使用 JSON.pretty_generate(final) 但它不工作,抛出错误:

only generation of JSON objects or arrays allowed (JSON::GeneratorError)

将每个机器人转换为 JSON 并在其开头添加机器人名称的最佳方法是什么?

最佳答案

def generate_asn_bot
config = YAML.load_file('my_conf.yaml')
asn = config["conf_file"]["asn_lookup"]
hash = Hash.new
hash["asn-lookup"] = asn
puts JSON.pretty_generate(hash)
end

刚刚将所有内容保存到哈希中!

关于json - YAML 到 JSON Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42226922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com