简体   繁体   中英

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.

Sample JSON response:

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

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

.lastAccessStr.elapsedTimeMs.clientName (if available in output).requestID (if available in output).agentAddr (if available in output).localAddr (if available in output)

I have been trying to make this work using https://jqplay.org without any luck.

Can anyone give me some examples on how I would go about making this work?

User pmf has got it in the comments :

Use ? to ignore an error, and // to provide an alternative if the first one is null , false or inexistent. Define every column at question along the lines of (.clientConnInfo.clientName? // "none")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM