I am working on creating an adaptive card dynamically. I have the skeleton on the card in JSON and I want to add entries to it (each entry will be a JSON object inside the "body" of the below schema). Add new element to existing JSON array with jq shows how to add the element but in that question the whole element is inside the jq command but in my case since the element is very long, it is a variable & I am not able to figure how to add the element using the variable.
Below is the "schema" JSON:
{
"type": "AdaptiveCard",
"$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5",
"body": [
{
"type": "TextBlock",
"text": "Daily Dashboard",
"wrap": true,
"size": "Large",
"weight": "Bolder"
}
]
}
Below is my "one entry" JSON:
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "StellarEdge Outdoor Equipment",
"size": "Default",
"weight": "Bolder",
"maxLines": 4,
"wrap": true
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Badge",
"text": "Success",
"size": "Large",
"style": "Good",
"appearance": "Tint",
"icon": "CheckmarkCircle",
"horizontalAlignment": "Right"
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.OpenUrl",
"title": "Action.OpenUrl",
"url": "avvb"
}
]
}
]
}
],
"targetWidth": "AtLeast:Standard"
}
I want to add many "updated" one_entries into my schema. This is what I am doing now
Create a temporary file and store its name in a variable (I do this because I do not want to pollute schema or the one entry JSON files)
# read schema into a temp file
TEMP_FILE=$(mktemp ./file.XXXXXX)
jq '.' schema.json >> $TEMP_FILE
# read one_entry nito a variable
updated_entry=$(jq -r '.' one_entry.json)
# update the needed fields in the variable
updated_entry=$(echo $entry | jq '.columns[0].items[0].text = "Issues"' | jq '.columns[1].items[0].style = "Warning"' | jq '.columns[1].items[0].text = "Issues"' | jq '.columns[2].items[0].actions[0].url = "google.com"')
# try to add the updated one_entry into the schema file
jq -r '.' $TEMP_FILE | jq --arg UPDATED_ENTRY "$updated_entry" '.body += [$UPDATED_ENTRY]' >> abc.json
I will use the abc.json later for a curl command. Also, the TEMP_FILE will need to be updated multiple times (I can write the abc file in the end, that is not an issue).
However I am failing in adding the updated one entry to TEMP_FILE.
My expected output is something like below. As you can see, inside the "body" array, after the frist element, all other elements are modifications of the one_entry JSON. (If you copy and paste the below JSON in the "CARD PAYLOAD EDITOR" of this page, you will see what I am trying to achieve.)
{
"type": "AdaptiveCard",
"$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5",
"body": [
{
"type": "TextBlock",
"text": "Credit limit details",
"wrap": true,
"size": "Large",
"weight": "Bolder"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "StellarEdge Outdoor Equipment",
"size": "Default",
"weight": "Bolder",
"maxLines": 4,
"wrap": true
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Badge",
"text": "Success",
"size": "Large",
"style": "Good",
"appearance": "Tint",
"icon": "CheckmarkCircle",
"horizontalAlignment": "Right"
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.OpenUrl",
"title": "Action.OpenUrl",
"url": "avvb"
}
]
}
]
}
],
"targetWidth": "AtLeast:Standard"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "StellarEdge Outdoor Equipment",
"size": "Default",
"weight": "Bolder",
"maxLines": 4,
"wrap": true
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Badge",
"text": "Issues",
"size": "Large",
"style": "Warning",
"appearance": "Tint",
"icon": "Warning",
"horizontalAlignment": "Right"
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.OpenUrl",
"title": "Action.OpenUrl",
"url": "abcd"
}
]
}
]
}
],
"targetWidth": "AtLeast:Standard"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": "StellarEdge Outdoor Equipment",
"size": "Default",
"weight": "Bolder",
"maxLines": 4,
"wrap": true
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Badge",
"text": "Failure",
"size": "Large",
"style": "Attention",
"icon": "PresenceOffline",
"horizontalAlignment": "Right",
"appearance": "Tint"
}
]
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.OpenUrl",
"title": "Action.OpenUrl",
"url": "ffgf"
}
]
}
]
}
],
"targetWidth": "AtLeast:Standard"
}
]
}
echo $entry|Action.OpenUrlI find it is much simpler to just[text](url)include the url in the markdown text. And overall the whole job might be simpler in python or perl, than in shell+jq.jq '.body += [input | .columns[0].items[0].text = "Issues" | .columns[1].items[0].style = "Warning" | .columns[1].items[0].text = "Issues" | .columns[2].items[0].actions[0].url = "google.com"]' schema.json one_entry.jsondo what you want? Demo