[英]How do I use jq to convert nested JSON output to CSV?
I have a JSON output that I receive from cURL, I would like to pipe to jq to extract useful information and display the csv data in a terminal. I have a JSON output that I receive from cURL, I would like to pipe to jq to extract useful information and display the csv data in a terminal.
Sample JSON response:样品 JSON 响应:
{
"operation": "GET CLIENT SESSIONS",
"outcome": "SUCCESS",
"result": {
"Session": [
{
"sessionID": "E1B7190EF32AA9F6E63265ADBB88D1A9D6F4254457A0.production",
"requestID": "",
"sessionPoolID": "8LNIVzRTSo-jJV-CdPMP0Q",
"agentID": "",
"ablSessionID": "",
"lastAccessStr": "2022-04-01T10:36:14.745-0700",
"elapsedTimeMs": 1675892,
"sessionState": "AVAILABLE",
"requestState": "READY",
"sessionType": "SESSION_FREE",
"adapterType": "APSV",
"bound": false,
"clientConnInfo": null,
"agentConnInfo": null
},
{
"sessionID": "E1B7190EF32AA9F6E63265ADBB88D1A9D6F4254457A0.production",
"requestID": "",
"sessionPoolID": "8LNIVzRTSo-jJV-CdPMP0Q",
"agentID": "",
"ablSessionID": "",
"lastAccessStr": "2022-04-01T10:36:14.745-0700",
"elapsedTimeMs": 1675892,
"sessionState": "AVAILABLE",
"requestState": "READY",
"sessionType": "SESSION_FREE",
"adapterType": "APSV",
"bound": false,
"clientConnInfo": null,
"agentConnInfo": null
}
]
},
"errmsg": "",
"versionStr": "v1.0.0 ( 2021-10-29 )",
"versionNo": 1
}
I am able to extract information using this query:我可以使用此查询提取信息:
jq '.result.Session[] | ([.lastAccessStr, .elapsedTimeMs, .clientConnInfo]) | @csv'
The problem I am running into, sometimes the response can have additional nested data for clientConnInfo and agentConnInfo as seen in the example below.我遇到的问题,有时响应可能包含 clientConnInfo 和 agentConnInfo 的其他嵌套数据,如下例所示。
{
"operation": "GET CLIENT SESSIONS",
"outcome": "SUCCESS",
"result": {
"Session": [
{
"sessionID": "E1B7190EF32AA9F6E63265ADBB88D1A9D6F4254457A0.production",
"requestID": "",
"sessionPoolID": "8LNIVzRTSo-jJV-CdPMP0Q",
"agentID": "",
"ablSessionID": "",
"lastAccessStr": "2022-04-01T10:36:14.745-0700",
"elapsedTimeMs": 1675892,
"sessionState": "AVAILABLE",
"requestState": "READY",
"sessionType": "SESSION_FREE",
"adapterType": "APSV",
"bound": false,
"clientConnInfo": null,
"agentConnInfo": null
},
{
"sessionID": "26691913A73E55175D233F86D219B4AEFA4AD14AE9E4.production",
"requestID": "ROOT:a:000002a6",
"sessionPoolID": "8LNIVzRTSo-jJV-CdPMP0Q",
"agentID": "qsrLXAxsRRanJio6dYOC2Q",
"ablSessionID": "",
"lastAccessStr": "2022-04-01T11:04:08.902-0700",
"elapsedTimeMs": 1735,
"sessionState": "RESERVED",
"requestState": "RUNNING",
"sessionType": "SESSION_FREE",
"adapterType": "APSV",
"bound": false,
"clientConnInfo": {
"clientName": "xxxxx",
"requestID": "ROOT:a:000002a6",
"sessionID": "26691913A73E55175D233F86D219B4AEFA4AD14AE9E4.production",
"adapterType": "APSV",
"reqStartTimeStr": "2022-04-01T11:04:08.902-0700",
"elapsedTimeMs": 1735,
"executerThreadId": "thd-8",
"requestUrl": "xxxxx",
"requestProcedure": "xxxxx.p",
"httpSessionId": "26691913A73E55175D233F86D219B4AEFA4AD14AE9E4.production"
},
"agentConnInfo": {
"agentID": "qsrLXAxsRRanJio6dYOC2Q",
"connID": "AR2k7gnYSiKPCk2DEHpaSg",
"connPoolID": "v0Lh7XcITsOSfoF_RjXXig",
"state": "RESERVED",
"agentAddr": "xxxxx",
"localAddr": "xxxxx"
}
}
]
},
"errmsg": "",
"versionStr": "v1.0.0 ( 2021-10-29 )",
"versionNo": 1
}
If I try to use the same query, it dies with below error:如果我尝试使用相同的查询,它会因以下错误而死:
jq: error (at <stdin>:0): object ({"clientNam...) is not valid in a csv row
The desired output is to capture this:所需的 output 是为了捕获这个:
.lastAccessStr.elapsedTimeMs.clientName (if available in output).requestID (if available in output).agentAddr (if available in output).localAddr (if available in output) .lastAccessStr.elapsedTimeMs.clientName(如果在输出中可用).requestID(如果在输出中可用).agentAddr(如果在输出中可用).localAddr(如果在输出中可用)
I have been trying to make this work using https://jqplay.org without any luck.我一直在尝试使用https://jqplay.org来完成这项工作,但没有任何运气。
Can anyone give me some examples on how I would go about making this work?谁能给我一些例子,说明我将如何让 go 完成这项工作?
User pmf has got it in the comments :用户pmf 在评论中得到了它:
Use
?
使用
?
to ignore an error, and//
to provide an alternative if the first one isnull
,false
or inexistent.忽略错误,并且
//
如果第一个错误是null
、false
或不存在,则提供替代方法。 Define every column at question along the lines of(.clientConnInfo.clientName? // "none")
按照
(.clientConnInfo.clientName? // "none")
的行定义问题中的每一列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.