【问题标题】:Trying to use jq to format json data on one line尝试使用jq在一行上格式化json数据
【发布时间】:2021-06-01 22:06:20
【问题描述】:

下面的输出是我的 curl 查询的 json 输出。我尝试了多个 jq 过滤器来尝试获得看起来像的结果 interfaceId、供应商、地址

从我的 json/curl 输出,应用“jq”过滤器后,我只得到:

eth1

eth1

eth1

Apple

BB Technologies (Limited)

null

Ipaddress#1

Ipaddress#2

Ipaddress#3

我希望输出是:

interfaceId,vendor,addresses curl/json 返回的每条记录

有人可以帮我解决这个问题

{
  "deviceId": "75c2fde3-4b5a-4faf-a24d-d8019e8911e1",
  "macTable": [
    {
      "interfaceId": "eth1",
      "addresses": [
        "192.168.5.101"
      ],
      "mac": "3c:15:c2:c3:f0:e0",
      "vendor": "Apple",
      "vlan": null,
      "hostname": null,
      "device": {
        "id": null,
        "connected": null,
        "enabled": null,
        "authorized": null,
        "ipAddress": null,
        "identification": {
          "id": null,
          "type": null
        },
        "overview": {
          "status": "unknown"
        }
      },
      "site": null
    },
    {
      "interfaceId": "eth1",
      "addresses": [
        "192.xxx.x.1"
      ],
      "mac": "xx:83:c4:01:ac:5d",
      "vendor": "BB Technologies (Limited)",
      "vlan": null,
      "hostname": null,
      "device": {
        "id": null,
        "connected": null,
        "enabled": null,
        "authorized": null,
        "ipAddress": null,
        "identification": {
          "id": null,
          "type": null
        },
        "overview": {
          "status": "unknown"
        }
      },
      "site": null
    },
    {
      "interfaceId": "eth1",
      "addresses": [
        "192.xxx.x.100"
      ],
      "mac": "9f:25:0b:4b:32:73",
      "vendor": null,
      "vlan": null,
      "hostname": null,
      "device": {
        "id": null,
        "connected": null,
        "enabled": null,
        "authorized": null,
        "ipAddress": null,
        "identification": {
          "id": null,
          "type": null
        },
        "overview": {
          "status": "unknown"
        }
      },
      "site": null
    }
  ],
  "pagination": {
    "total": 3,
    "count": 20,
    "page": 1,
    "pages": 1
  }
}

【问题讨论】:

    标签: json export-to-csv jq


    【解决方案1】:
    jq -r '.macTable[] | [.interfaceId, .vendor, .addresses[]] | @csv' x.json
    

    输出类似于

    "eth1","Apple","192.168.5.101"
    "eth1","BB Technologies (Limited)","192.xxx.x.1"
    "eth1",,"192.xxx.x.100"
    

    文档是https://stedolan.github.io/jq/manual/#Builtinoperatorsandfunctions

    【讨论】:

    • 谢谢。我明白我现在的问题是什么了。感谢您的快速回复。
    猜你喜欢
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    相关资源
    最近更新 更多