使用“jq”从 JSON 文件导出所需的输出(按正确顺序的字段)

Posted

技术标签:

【中文标题】使用“jq”从 JSON 文件导出所需的输出(按正确顺序的字段)【英文标题】:Export needed output (fields in right order) from JSON file using "jq" 【发布时间】:2022-01-18 05:07:41 【问题描述】:

我尝试使用 jq 从 json 文件中 grep 所需的字段和参数。有json文件:

[
  
    "ip": "1.11.11.111",
    "services": [
      
        "port": 22,
        "service_name": "SSH",
        "transport_protocol": "TCP"
      ,
      
        "port": 888,
        "service_name": "HTTP",
        "transport_protocol": "TCP"
      ,
      
        "port": 999,
        "service_name": "FTP",
        "transport_protocol": "TCP"
      ,
      
        "port": 17000,
        "service_name": "UNKNOWN",
        "transport_protocol": "TCP"
      
    ],
    "location": 
      "continent": "Europe",
      "country": "France",
      "country_code": "FR",
      "city": "Cagnes-sur-Mer",
      "postal_code": "06800",
      "timezone": "Europe/Paris",
      "province": "Provence-Alpes-Côte d'Azur",
      "coordinates": 
        "latitude": 43.6627,
        "longitude": 7.1545
      ,
      "registered_country": "France",
      "registered_country_code": "FR"
    ,
    "autonomous_system": 
      "asn": 3215,
      "description": "France Telecom - Orange",
      "bgp_prefix": "2.15.0.0/16",
      "name": "France Telecom - Orange",
      "country_code": "FR"
    ,
    "operating_system": 
      "uniform_resource_identifier": "cpe:2.3:o:*:linux:*:*:*:*:*:*:*:*",
      "part": "o",
      "product": "linux",
      "source": "OSI_TRANSPORT_LAYER"
    
  ,
  
    "ip": "2.22.22.222",
    "services": [
      
        "port": 4444,
        "service_name": "HTTP",
        "transport_protocol": "TCP"
      ,
      
        "port": 8085,
        "service_name": "HTTP",
        "transport_protocol": "TCP"
      
    ],
    "location": 
      "continent": "Europe",
      "country": "United Kingdom",
      "country_code": "GB",
      "city": "Scunthorpe",
      "postal_code": "DN16",
      "timezone": "Europe/London",
      "province": "England",
      "coordinates": 
        "latitude": 53.5651,
        "longitude": -0.6375
      ,
      "registered_country": "United Kingdom",
      "registered_country_code": "GB"
    ,
    "autonomous_system": 
      "asn": 12576,
      "description": "EE Ltd",
      "bgp_prefix": "2.25.0.0/16",
      "name": "EE Ltd",
      "country_code": "GB"
    
  
]

所以,我想选择每个 ip 的 HTTP 端口,并以该格式输出:

1.11.11.111:888
2.22.22.222:4444
2.22.22.222:8085

我尝试执行下一个命令:

jq '.[].ip, .[].services[0].port, .[].services[0].service_name'

但我收到了:

"1.11.11.111"
"2.22.22.222"
"3.33.33.333"
6001
22
4444
3333
2082
80
80
9180
"HTTP"
"SSH"
"HTTP"
"FTP"

【问题讨论】:

【参考方案1】:

试试

jq -r '.[] | .ip + ":\(.services[] | select(.service_name == "HTTP") | .port)"'
1.11.11.111:888
2.22.22.222:4444
2.22.22.222:8085

Demo

【讨论】:

以上是关于使用“jq”从 JSON 文件导出所需的输出(按正确顺序的字段)的主要内容,如果未能解决你的问题,请参考以下文章

使用搅拌机,设计行走的人动画并导出为 JSON 所需的编码步骤是啥

webservice的输出没有使用alamofire快速提供所需的json

所需的 JSON 文件具有运行程序之前的旧值

gulp.src() 没有读取所需的 JSON 文件的数组值

gulp.src() 没有读取所需的 JSON 文件的数组值

使用 MediaInfo 命令行和 jq 从文件目录构建有效的 JSON 播放列表