我有 data.json 看起來像這樣
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "Monitoring",
"wrap": true,
"weight": "bolder",
"size": "large"
},
{
"type": "TextBlock",
"text": "23-04-2022 20:00",
"wrap": true,
"size": "Small",
"isSubtle": true
},
{
"type": "RichTextBlock",
"inlines": [
]
}
],
"": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5"
}
我正在嘗試將值動態添加到行內陣列
示例腳本
#!/bin/bash
cluster="live"
echo "$(jq --arg c "$cluster" '.body[2].inlines[2] = {"type": "TextRun","text": "Error: Failed Event in Activity Log $c" }' data.json)" > data.json
echo "$(jq '.body[2].inlines[2] = {"type": "TextRun","text": "VMs are in running state" }' data.json)" > data.json
echo "$(jq '.body[2].inlines[2] = {"type": "TextRun","text": "VMs are NOT in running state" }' data.json)" > data.json
echo "$(jq '.body[2].inlines[2] = {"type": "TextRun","text": "VMs are OFCOURSE in running state" }' data.json)" > data.json
我有2個問題。
首先:我嘗試找到在陣列中使用動態值的解決方案,但它沒有擴展它。
第二:只插入最后一個值,前一個值不知何故變成了null
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "OU Monitoring",
"wrap": true,
"weight": "bolder",
"size": "large"
},
{
"type": "TextBlock",
"text": "23-04-2022 20:00",
"wrap": true,
"size": "Small",
"isSubtle": true
},
{
"type": "RichTextBlock",
"inlines": [
null, <==
null, <==
{
"type": "TextRun",
"text": "VMs are OFCOURSE in running state"
}
]
}
],
"": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5"
}
我是 jq 的新手,因此任何有關解釋的幫助都會對我有很大幫助。提前致謝 :)
uj5u.com熱心網友回復:
第一個問題:如果您有一個空陣列,則在索引處設定一個值2(與 一樣.body[2].inlines[2]),之前的(即索引0和1)將設定為null。
第二個問題:你總是修改同一個專案(即.body[2].inlines[2])。 =在這種情況下,會將新物件附加到現有物件,但由于它使用相同的鍵,它們將被覆寫。
解決方案: =在陣列上使用,并將新專案括在括號中(使其成為另一個包含一項的陣列)。這樣,您可以連續填充現有陣列。
.body[2].inlines = [{"type": "TextRun","text": "Error: Failed Event in Activity Log $c"}]
演示
您可能還希望將您的操作放在一個jq呼叫中。要么使用管道|連接單個操作,要么,特別是在這種情況下,在添加陣列時一次添加所有專案。
cluster="live"
jq --arg c "$cluster" '
.body[2].inlines = [{"type": "TextRun","text": "Error: Failed Event in Activity Log $c"}]
| .body[2].inlines = [{"type": "TextRun","text": "VMs are in running state"}]
| .body[2].inlines = [{"type": "TextRun","text": "VMs are NOT in running state"}]
| .body[2].inlines = [{"type": "TextRun","text": "VMs are OFCOURSE in running state"}]
' data.json
演示
cluster="live"
jq --arg c "$cluster" '
.body[2].inlines = [
{"type": "TextRun","text": "Error: Failed Event in Activity Log $c"},
{"type": "TextRun","text": "VMs are in running state"},
{"type": "TextRun","text": "VMs are NOT in running state"},
{"type": "TextRun","text": "VMs are OFCOURSE in running state"}
]
' data.json
演示
輸出:
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "Monitoring",
"wrap": true,
"weight": "bolder",
"size": "large"
},
{
"type": "TextBlock",
"text": "23-04-2022 20:00",
"wrap": true,
"size": "Small",
"isSubtle": true
},
{
"type": "RichTextBlock",
"inlines": [
{
"type": "TextRun",
"text": "Error: Failed Event in Activity Log $c"
},
{
"type": "TextRun",
"text": "VMs are in running state"
},
{
"type": "TextRun",
"text": "VMs are NOT in running state"
},
{
"type": "TextRun",
"text": "VMs are OFCOURSE in running state"
}
]
}
],
"": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463396.html
上一篇:在R中本地保存.json檔案
下一篇:雪花json檢索具有特定值的標簽
