在 jq bash 中获取空值

Getting null values in jq bash

我有 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

我有两个问题。

首先:我尝试寻找在数组中使用动态值的解决方案,但它没有扩展它。

second: 只插入了最后一个值,之前的值不知何故变成了空值

{
  "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"
}

第一个问题:如果你有一个空数组,然后在索引 2 处设置一个值(与 .body[2].inlines[2] 一样),之前的值(即索引 01) 将设置为 null.

第二期:你总是修改同一个项目(即.body[2].inlines[2])。 += 在这种情况下会将新对象附加到现有对象,但由于它使用相同的键,它们将被覆盖。

解决方案:在数组上使用 +=,并将新项括在方括号中(使其成为另一个包含一项的数组)。这样你就可以连续填充现有的数组。

.body[2].inlines += [{"type": "TextRun","text": "Error: Failed Event in Activity Log $c"}]

Demo

您可能还希望将您的操作放在一次 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

Demo

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

Demo

输出:

{
  "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"
}