使用 Logstash 删除 Json 对象名称

Removing Json object name using Logtash

我正在尝试使用 logstash

解析以下 json
   {
    "payload": "{\"class\":\"OpenLoyalty\\Component\\Account\\Domain\\Event\\PointsTransferHasBeenExpired\",\"payload\":{\"accountId\":\"1cbd42b8-1b1f-4a13-a28f-eefeb73a64b0\",\"pointsTransferId\":\"e82c96cf-32a3-43bd-9034-4df343e5f111\"}}"
}

使用此过滤器:

    input {
        jdbc {
            jdbc_connection_string => "jdbc:postgresql://localhost:5432/openloyalty"
            jdbc_user => "openloyalty"
            jdbc_password => "openloyalty"
            jdbc_driver_library => "C:\logstash\postgresql-42.2.12.jar"
            jdbc_driver_class => "org.postgresql.Driver"
            statement => "SELECT payload from events where events.id=130;"
        }
    }
    filter {
          json {
            source => "payload"
            remove_field => ["class"]
          }

    }
    output {
        stdout { codec => json_lines }
    }

我得到这个输出:

    {
    "@version": "1",
    "@timestamp": "2020-05-12T11:27:15.097Z",
    "payload": {
        "accountId": "1cbd42b8-1b1f-4a13-a28f-eefeb73a64b0",
        "pointsTransferId": "e82c96cf-32a3-43bd-9034-4df343e5f111"
    }
   }

然而,我要找的是这样的:

{
"@version": "1",
"@timestamp": "2020-05-12T11:27:15.097Z",
 {
    "accountId": "1cbd42b8-1b1f-4a13-a28f-eefeb73a64b0",
    "pointsTransferId": "e82c96cf-32a3-43bd-9034-4df343e5f111"
 }
}

如何从我的 Json 对象中删除 "payload" 名称?

您可以在 json 过滤器之后使用具有以下配置的 mutate 过滤器。

mutate {
    rename => {
        "[payload][accountId]" => "accountId"
        "[payload][pointsTransferId]" => "pointsTransferId"
    }
    remove_field => ["[payload]"]
}

这将重命名您的字段并在重命名后删除空的 payload json 对象。