我有一个基于JSON的Winston Log,其中包含多个对话结构的JSON结构。
每个对话基本上都从一个简单的“ info”消息开始,并以一个“ result”消息结束。.但是,在这两种消息之间有4种消息“ request”类型之一和详细消息。
我要输出的是“信息”中的3个字段,“请求”中的3个字段和“结果”中的3个字段。现在,尽管我在输出中得到了一堆NULL,因为我在每个调用中都输出了所有消息请求。
所以我想弄清楚的是,如何将4种不同的请求类型基本上合并为3个一致的字段,以及添加文本标签。
tocsv(inputs
| ### Header (3 Fields)
[.timestamp, .level, .message]
#TODO: Add a conditional Here and Add Type Static Text
### Create (3 fields)
#+ "Create"
if .Create then
+ ( .Create .Header | [.TID, .TimeStamp] )
+ ( .Create .CreateRequest .Service | [.Customer] )
### Update (3 fields)
#+ "Update"
elif .Update then
+ ( .Update .Header | [.TID, .TimeStamp] )
+ ( .Update .Service | [.Customer] )
### Cancel (3 fields)
# + "Cancel"
elif .Cancel then
+ ( .Cancel .Header | [.TID, .TimeStamp] )
+ ( .Cancel .Service | [.Customer] )
### Result (3 fields)
+ ( .result | [.TID, .TimeStamp, .Status] )
### End (15 total fields; most being null)
)
我也尝试过Select (.Create != null)
,但收到错误消息“ jq:error(at:13):object({” Create ...)和array([“ 666b4e10 -...)已添加
tocsv(inputs
| ### Header (3 Fields)
[.timestamp, .level, .message]
#TODO: Add a conditional Here and Add Type Static Text
### Create (3 fields)
#+ "Create"
+ ( select (.Create .Header != null)
+ ( .Create .Header | [.TID, .TimeStamp] )
+ ( .Create .CreateRequest .Service | [.Customer] ))
### Update (3 fields)
#+ "Update"
+ ( select (.Update .Header != null)
+ ( .Update .Header | [.TID, .TimeStamp] )
+ ( .Update .Service | [.Customer] ))
### Cancel (3 fields)
# + "Cancel"
+ (select (.Cancel .Header != null)
+ ( .Cancel .Header | [.TID, .TimeStamp] )
+ ( .Cancel .Service | [.Customer] ))
### Result (3 fields)
+ ( .result | [.TID, .TimeStamp, .Status] )
### End (15 total fields; most being null)
)
如果我删除条件语句,当前输出如下:
"200","2018-07-18T16:07:33.047Z","info","SOAP createServiceRequest ack recieved & is successful.","null","null","null","null","null","null","null","null","null","ac710dc0-8aa4-11e8-a08c-0d98209bb4f5","2018-07-18T12:07:33.082-04:00","Accepted"
因此,在上面的代码中,我想要做的就是将变体折叠到4个字段中(包括将基于类型的文本插入到不在JSON中的自己的字段中),并希望将其生产看起来像这样:
"200","2018-07-18T16:07:33.047Z","info","SOAP createServiceRequest ack recieved & is successful.","Create/Update/Cancel","value","value","value","ac710dc0-8aa4-11e8-a08c-0d98209bb4f5","2018-07-18T12:07:33.082-04:00","Accepted"
似乎可能是Reduce
,但我不想添加或累加值。.基本上只在CSV输出中共享位置。
我要解决的主要问题是将上下文输出合并到合并字段中,如何完成我要完成的工作?