简体   繁体   English

解析通过 curl 下载的 json 列表中的双引号值之间的值

[英]Parsing values between double quotes values from json list downloaded via curl

I have an online JSON file.我有一个在线 JSON 文件。 Every time I try to download it.每次我尝试下载它。 But I need to parse this JSON and get online ip's between double-quotes.但我需要解析这个 JSON 并获取双引号之间的在线 ip。 Normally I can parse it with jq like .sample[].echo etc. but my list bigger than that example1,2..300 store more than 300 block data in it.通常我可以用 jq 解析它,比如 .sample[].echo 等,但我的列表比 example1,2..300 大,其中存储了 300 多个块数据。 I need to parse and get only inside of double quotes and get the all ip's.我需要解析并仅获取双引号内的内容并获取所有 ip。

By the way: I saw many questions on stackover but none of them related to filtering this kind of input顺便说一句:我在stackover上看到了很多问题,但没有一个与过滤这种输入有关

curl -L "https://sample.data.com/ip.json" -o 'ip.json' | *need to parsing method here*

Here is my JSON file:这是我的 JSON 文件:

{
  "sample1": {
    "echo": [
      "255.254.253.0/32"
    ],
    "delta": [
      "199.231.211.0/32"
    ]
  },
    "sample2": {
    "beta": [
      "250.123.124.0/32"
    ],
    "pre": [
      "122.156.243.0/32"
    ]
  },
  .
  .
  .
  .
  .
}

If you want to extract all strings that looks like an IP, then you can still use jq .如果您想提取所有看起来像 IP 的字符串,那么您仍然可以使用jq If we "fix" your example JSON above, to be actual JSON, then running the following jq program:如果我们“修复”您上面的示例 JSON,使其成为实际的 JSON,则运行以下 jq 程序:

.. | strings | match("\\d+\\.\\d+\\.\\d+\\.\\d+(?:/\\d+)?").string

over it would output:它会输出:

"255.254.253.0/32"
"199.231.211.0/32"
"250.123.124.0/32"
"122.156.243.0/32"

I guess your don't want to keep the IPs quored, this can be handled with -r output flag我猜您不想保留 IP,这可以使用-r输出标志来处理

$ curl ... | jq -r '.. | strings | match("\\d+\\.\\d+\\.\\d+\\.\\d+(?:/\\d+)?").string'
255.254.253.0/32
199.231.211.0/32
250.123.124.0/32
122.156.243.0/32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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