簡體   English   中英

使用 JQ 解析 JSON 並重建 JSON

[英]Parsing JSON with JQ & reconstruct JSON

對於我需要從 JSON 開始的項目,我轉向了 JQ,因為我的腳本的其余部分也在 bash 中。 我的主要問題是我無法解析我的輸入,因為該文件由幾個 JSON 組成:

{"url":"https://www.domain.tld:443","content-length":11089,"status-code":200,"location":"","title":"","vhost":false,"webserver":"Netlify","content-type":"text/html","tls":{"dns_names":["domain.tld","www.domain.tld"],"common_name":["domain.tld","Let's Encrypt Authority X3"],"organization":["Let's Encrypt"],"issuer_common_name":["Let's Encrypt Authority X3","DST Root CA X3"],"issuer_organization":["Let's Encrypt","Digital Signature Trust Co."]},"csp":{"domains":["https://www.domain.tld:443"]},"http2":false,"method":"GET"}
{"url":"http://www.domain.tld:80","content-length":37,"status-code":301,"location":"https://www.domain.tld/","title":"","vhost":false,"webserver":"Netlify","content-type":"text/plain","csp":{"domains":["https://www.domain.tld:443"]},"http2":false,"method":"GET"}
{"url":"https://domain.tld:443","content-length":37,"status-code":301,"location":"https://www.domain.tld/","title":"","vhost":false,"webserver":"Netlify","content-type":"text/plain","tls":{"dns_names":["domain.tld","www.domain.tld"],"common_name":["domain.tld","Let's Encrypt Authority X3"],"organization":["Let's Encrypt"],"issuer_common_name":["Let's Encrypt Authority X3","DST Root CA X3"],"issuer_organization":["Let's Encrypt","Digital Signature Trust Co."]},"csp":{"domains":["https://www.domain.tld:443"]},"http2":false,"method":"GET"}
{"url":"http://domain.tld:80","content-length":33,"status-code":301,"location":"https://domain.tld/","title":"","vhost":false,"webserver":"Netlify","content-type":"text/plain","csp":{"domains":["https://www.domain.tld:443"]},"http2":false,"method":"GET"}

最終目標是提取 URL、狀態代碼和內容長度,以重建 JSON 以作為 POST 發送到另一個 API。

另一個問題是我可以使用cat file.json | jq .url正確提取 url cat file.json | jq .url 例如cat file.json | jq .url但不是cat file.json | jq .status-code的狀態碼cat file.json | jq .status-code cat file.json | jq .status-code : jq: error: code/0 is not defined at <top-level>, line 1:

任何的想法 ?
謝謝

對於特殊字符問題:您想將引用它的信息傳遞給jq ,因此您想繞過 bash 的引用縮減。 另外,請注意我不使用cat ,因為它在這里是 unnessecary :

jq '."status-code"' file.json

對於多個json字符串問題:

之前在整個文件上的同一行代碼給了我以下結果:

200
301
301
301

我可以是jq的版本,我的是jq-1.5-1-a5b5cbe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM