Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting null values in jq bash

I have data.json which looks like this

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

And I’m trying to add values dynamically to the inlines array

sample script

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/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

I have got 2 problems.

first: I have tried finding solution to use dynamic values in the array but it doesn’t expands it.

second: Only the last value is inserted, the previous value somehow becomes 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"
}

I’m new to jq hence any help with some explanation would help me a lot.
Thanks in advance 🙂

>Solution :

First issue: If you have an empty array, then set a value at index 2 (as with .body[2].inlines[2]), the previous ones (namely index 0 and 1) will be set to null.

Second issue: You always modify the same item (namely .body[2].inlines[2]). += in this case will append the new object to the existing one, but as it’s using the same keys, they will be overwritten.

Solution: Use += on the array, and wrap the new item in brackets (making it another array of one item). This way you can successively populate the existing array.

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

Demo

You also may want to put your operations into just one jq call. Either use the pipe | to concatenate the single operations, or, especially in this case, add all items at once as you are adding arrays anyways.

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

Output:

{
  "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"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading