【问题标题】:jq query format csv exportjq查询格式csv导出
【发布时间】:2019-01-15 04:23:29
【问题描述】:

我有一个带有 JQ 的脚本 bash,但不知道如何执行查询以获得以下结果:

conversationId(1),participantId(1),participantName(1),sessionId(1) conversationId(1),participantId(2),participantName(2),sessionId(2) conversationId(1),participantId(3),participantName(3),sessionId(3) conversationId(2),participantId(4),participantName(4),sessionId(4) . . . conversationId(n),participantId(n),participantName(n),sessionId(n)

我的文件:data.json

{
    "conversations": [
        {
            "conversationId": "cxx-cccc-4444-9999-b11111111",
            "conversationStart": "2016-06-5T00:10:15.412Z",
            "conversationEnd": "2016-06-5T00:11:19.447Z",
            "participants": [
                {
                    "participantId": "2aaaa-1555-4590-99aa-4yyyyyyy",
                    "userId": "91111-7rrrr-4000-aqaa-23232323",
                    "purpose": "user",
                    "sessions": [
                        {
                            "mediaType": "voice",
                            "sessionId": "1111111-aaaa-4009-9000-488888",
                            "ani": "sip:user+localhost.com@localhost",
                            "direction": "outbound",
                            "dnis": "tel:+1111110000",
                            "edgeId": "e3e3e3-4ª4q-4b4b-9792-95959595",
                            "segments": [
                                {
                             "segmentStart": "2016-06-15T00:10:15.412Z",
                             "segmentEnd": "2016-06-15T00:10:10.485Z",
                            "segmentType": "contacting",
                                    "conference": false
                                },
                                {
                             "segmentStart": "2016-06-15T00:10:10.485Z",
                             "segmentEnd": "2016-06-15T00:10:10.522Z",
                              "segmentType": "dialing",
                               "conference": false
                                },
                                {
                              "segmentStart": "2016-06-15T00:20:10.522Z",
                              "segmentEnd": "2016-06-15T00:11:09.436Z",
                              "disconnectType": "client",
                               "segmentType": "interact",
                                "conference": false
                                }
                            ]
                        }
                    ]
                },
                {
                 "participantId": "e9e9e9-d777-4a4a-8989-aeaeaeaeae",
                    "participantName": "Namek, Sayayin",
                    "purpose": "ivr",
                    "sessions": [
                        {
                            "mediaType": "voice",
                           "sessionId": "262626-6000-4cdd-a511-fafafafa",
                           "ani": "sip:goku%localhost.com@localhost",
                           "direction": "inbound",
                          "edgeId": "e3e3e3e3-4abc-4abc-9700-95959595",
                            "remoteNameDisplayable": "Namek, Sayayin",
                            "segments": [
                                {
                              "segmentStart": "2016-06-15T00:10:10.510Z",
                              "segmentEnd": "2016-06-15T00:10:10.521Z",
                              "segmentType": "system",
                                    "conference": false
                                },
                                {
                             "segmentStart": "2016-06-15T00:10:20.521Z",
                             "segmentEnd": "2016-06-15T00:11:39.447Z",
                              "disconnectType": "peer",
                                    "segmentType": "ivr",
                                    "conference": false
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "conversationId": "09090909-6b60-8888-xxxx-9yyyyyyyy",
            "conversationStart": "2016-06-15T00:11:38.867Z",
            "conversationEnd": "2016-06-15T01:54:55.744Z",
            "participants": [
                {
                    "participantId": "xxxc-cccc1-4123-7777-343434343e",
                    "userId": "9qwer-7zxc-40df-aghj-2323232323",
                    "purpose": "user",
                    "sessions": [
                        {
                            "mediaType": "voice",
                            "sessionId": "101010-0faz-4xxx-yyyy-4419441",
                            "ani": "sip:goku+localhost.com@localhost",
                            "direction": "outbound",
                            "dnis": "tel:+11112222333",
                            "edgeId": "nenene1-qwer-tyui-9000-9595gggh",
                            "segments": [
                               {
                              "segmentStart": "2016-06-15T00:11:18.867Z",
                              "segmentEnd": "2016-06-15T00:11:42.637Z",
                              "segmentType": "contacting",
                                "conference": false
                                }
                                ]
                        } ]
                } ]
        } ]
}

我的查询 jq:

jq 。 '{conversationId: .conversations[].conversationId, 参与者ID: .conversations[].participants[].participantId,participantName: .conversations[].participants[].participantName} |加入(“,”)'

但是conversationId 和participantName 字段会重复多次...

【问题讨论】:

  • 部分参与者没有姓名。
  • 抱歉,显然它没有格式化,我应该已经更正了,希望您能指导我
  • jq 具有内置 CSV 支持;见@csv。如果您尝试使用 join 来模拟它,那么极端情况(围绕引号、带有文字逗号或引号的字段等)将是错误的。
  • @CharlesDuffy 感谢您的观察

标签: json linux bash csv jq


【解决方案1】:

根据您的输入,以下过滤器:

.conversations[]
| [.conversationId]
  + (.participants[] | [.participantId, .participantName])

产生:

[
  "cxx-cccc-4444-9999-b11111111",
  "2aaaa-1555-4590-99aa-4yyyyyyy",
  null
]
[
  "cxx-cccc-4444-9999-b11111111",
  "e9e9e9-d777-4a4a-8989-aeaeaeaeae",
  "Namek, Sayayin"
]
[
  "09090909-6b60-8888-xxxx-9yyyyyyyy",
  "xxxc-cccc1-4123-7777-343434343e",
  null
]

变体

请注意,上述过滤器不使用任何 jq 变量。有时介绍它们会有所帮助,因此可能会感兴趣以下变体:

.conversations[]
| (.participants | range(0;length)) as $j
| [.conversationId]
  + (.participants[$j] | [.participantId, .participantName])

在jq手册的帮助下,你应该可以从这里拿到它。

附录

ParticipantId(1),sessionId(1),mediaType(1),sessionId(1),ani(1),dnis(1)

我不太清楚你为什么要sessionsId 两次,但以下对应于你的第二个请求:

.conversations[].participants[]
| [.participantId] +
   (.sessions[] | [.sessionId, .mediaType, .sessionId, .ani, .dnis])

【讨论】:

  • 嗨@peak 一个问题,如果我想进入“会话”并将“participantId”保留为初始字段....例如:ParticipantId(1),sessionId(1),mediaType(1 ),sessionId(1),ani(1),dnis(1) ..... 我欠你一条命.....
猜你喜欢
  • 2020-03-05
  • 2018-05-03
  • 2014-10-31
  • 2014-02-03
  • 2021-12-25
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
相关资源
最近更新 更多