如何从这个JSON文件中提取单词

如何从这个JSON文件中提取单词,json,jq,Json,Jq,我想从这个JSON文件中获取type结果。我该怎么做 { "inputs": [{ "title": "vault_beats", "global": true, "name": "Beats", "content_pack": null, "created_at": "2019-01-11T05:31:49.720Z", "type": "org.graylog.plugins.beats.

我想从这个JSON文件中获取
type
结果。我该怎么做

{
    "inputs": [{
        "title": "vault_beats",
        "global": true,
        "name": "Beats",
        "content_pack": null,
        "created_at": "2019-01-11T05:31:49.720Z",
        "type": "org.graylog.plugins.beats.Beats2Input",
        "creator_user_id": "admin",
        "attributes": {
            "recv_buffer_size": 1048576,
            "tcp_keepalive": false,
            "number_worker_threads": 1,
            "tls_client_auth_cert_file": "",
            "beats_prefix": false,
            "bind_address": "0.0.0.0",
            "tls_cert_file": "",
            "port": 5044,
            "tls_key_file": "admin",
            "tls_enable": false,
            "tls_key_password": "admin",
            "tls_client_auth": "disabled",
            "override_source": null
        },
        "static_fields": {},
        "node": null,
        "id": "5c382a45590801001d742017"
    }, {
        "title": "vault_beatsl",
        "global": true,
        "name": "GELF TCP",
        "content_pack": null,
        "created_at": "2019-01-11T05:32:05.869Z",
        "type": "org.graylog2.inputs.gelf.tcp.GELFTCPInput",
        "creator_user_id": "admin",
        "attributes": {
            "recv_buffer_size": 1048576,
            "tcp_keepalive": false,
            "use_null_delimiter": true,
            "number_worker_threads": 1,
            "tls_client_auth_cert_file": "",
            "bind_address": "0.0.0.0",
            "tls_cert_file": "",
            "decompress_size_limit": 8388608,
            "port": 12201,
            "tls_key_file": "admin",
            "tls_enable": false,
            "tls_key_password": "admin",
            "max_message_size": 2097152,
            "tls_client_auth": "disabled",
            "override_source": null
        },
        "static_fields": {},
        "node": null,
        "id": "5c382a55590801001d74202b"
    }],
    "total": 2
}
样本输出:-

"type":"org.graylog.plugins.beats.Beats2Input"
"type":"org.graylog2.inputs.gelf.tcp.GELFTCPInput"

需求有点模糊,但当与-r命令行选项一起使用时,以下命令会生成所需的输出,并且具有简单性的优点,但您可能希望对其进行调整以满足您的确切需求:

.. | objects | select(.type) | "\"type\":\"\(.type)\""

需求有点模糊,但当与-r命令行选项一起使用时,以下命令会生成所需的输出,并且具有简单性的优点,但您可能希望对其进行调整以满足您的确切需求:

.. | objects | select(.type) | "\"type\":\"\(.type)\""

如果它是经过修饰的JSON(这意味着
类型:
在单独的一行中),您只需要对它进行grep;它允许您定义行分隔符(RS)和字段分隔符(FS)。尝试类似于-
awk'BEGIN{RS=“,”;}1'one.json
的方法,它将所有键拆分为单独的行,然后grep所需的字段。将来,如果您在此处提出问题,请更精确地指定要求。展示您对解决方案的尝试通常也有助于澄清需求。与awk配合使用很好谢谢如果它是经过修饰的JSON(这意味着
类型:
在单独的一行中),您只需要对它进行grep;它允许您定义行分隔符(RS)和字段分隔符(FS)。尝试类似于-
awk'BEGIN{RS=“,”;}1'one.json
的方法,它将所有键拆分为单独的行,然后grep所需的字段。将来,如果您在此处提出问题,请更精确地指定要求。展示您在解决方案上的尝试通常也有助于澄清需求。awk的工作很好,谢谢