
需要明確的是- 我想要一個偽JSON 輸出,該輸出將被寫入此處的另一個靜態JSON 文件中...因此不包含在數組或任何內容中,只需在輸出中的每個實體後面取得遺失的逗號即可。
目前我的查詢是:
.[] | select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
哪個輸出:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
}
當我真正想要的時候:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
},
請注意每個實體後面的逗號!這讓我發瘋,我嘗試join(", ")
在不同的地方添加 a ,但這要么對最終輸出沒有任何作用,要么無法編譯,這取決於我放置它的位置。
這是包含資料的jqplayhttps://jqplay.org/s/xx3F_IWn03g
原始輸入 JSON:
[
{
"name": "minScore",
"expected": 0.8,
"actual": 1,
"values": [
1,
1,
1
],
"operator": ">=",
"passed": true,
"auditProperty": "performance",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.9,
"actual": 1,
"values": [
1,
1,
1
],
"operator": ">=",
"passed": true,
"auditProperty": "accessibility",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.9,
"actual": 0.96,
"values": [
0.93,
0.96,
0.96
],
"operator": ">=",
"passed": true,
"auditProperty": "best-practices",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.5,
"actual": 0.64,
"values": [
0.64,
0.64,
0.64
],
"operator": ">=",
"passed": true,
"auditProperty": "seo",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
}
]
答案1
你似乎想要調整auditId
已設定為 string 的條目categories
。你正在做的是提煉頂級數組中的這些元素並修改它們。這為您提供了一組(而不是數組)物件。
|
不要使用從數組中提取元素,而是使用|=
修改其元素。請參閱此答案的末尾,以了解如何將結果陣列正確插入到另一個檔案中的 JSON 結構中。
所以,使用
.[] |= (
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
或者,根本不使用.[]
而是使用map()
頂層數組中每個元素的表達式:
map(
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
這些表達式將為您提供一個如下所示的元素數組:
[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
}
]
array
若要將此陣列新增至另一個檔案中已存在的陣列中,您可以執行下列操作(假設的 頂層有一個鍵otherfile
,且您的輸入位於 中inputfile
;如果該鍵尚不存在,則會建立該鍵):
jq '.array += ( input |
map(
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
)' otherfile inputfile
執行時input
,將從命令列上的第二個檔案讀取數組。
如果您想要替換現有鍵的值而不是添加到現有鍵的值,請變更+=
為 just 。=
array
答案2
是的,這就是[]
迭代器的作用,每個迭代器都會獲得一個 JSON 檔案。要取得數組,您可以將整個表達式括起來[...]
以從中建立數組,或使用map()
它就地修改輸入數組,而不是[]
迭代其元素。
$ echo '[1,2,3,4]' | jq '.[]|select(.<3)'
1
2
$ echo '[1,2,3,4]' | jq '[.[]|select(.<3)]'
[
1,
2
]
$ echo '[1,2,3,4]' | jq 'map(select(.<3))'
[
1,
2
]
使用逗號分隔的元素而不包含[
,]
使其成為數組會使其無效 json,但如果這是您想要的,您可以透過管道將其sed '1d;$d'
刪除第一行和最後一行。