I have a JSON file payload.json:
{
"text":"Deployment started :rocket:",
"blocks":[
{
"type":"header",
"text":{
"type":"plain_text",
"text":":computer: Deployment release :computer:"
}
},
{
"type":"context",
"elements":[
{
"type":"mrkdwn",
"text":"*${{ steps.date.outputs.date }} | STAGING*"
}
]
},
{
"type":"divider"
},
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"PR_DESCRIPTION"
}
},
{
"type":"divider"
},
{
"type":"actions",
"elements":[
{
"type":"button",
"text":{
"type":"plain_text",
"text":"🚰 Pipeline"
},
"url":"${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}
]
}
],
"attachments":[
{
"color":"warning",
"fields":[
{
"title":"Statut",
"short":true,
"value":"Deploying ..."
}
]
}
]
}
And a txt file pr_body.txt:
### TITLE
**bold text**
- qsqds
- qsdqds
- qsdqsd
- qsdqsd
1. qsdqsd
2. qsdqsd
3. qsdqsd
4. qsdqsd
`some code here`
My goal is to replace PR_DESCRIPTION in payload.json with the formatted text in pr_body.txt
Is it possible to do that using jq?
I tried to use jq to update my JSON but due to array object I struggle to find the right way to do it.
I can do it in Python of course but it will be much future proof for me to do it using Shell commands.
Thank you in advance!
>Solution :
You can use the -R option to read in the markdown file as text, and the --argfile option to read in the JSON file. Then, select the nodes with path .blocks[].text.text and content "PR_DESCRIPTION", and set their values to the markdown input, previously stored in a variable:
< pr_body.txt jq -Rs --argfile json payload.json '. as $md | $json
| (.blocks[].text.text | select(. == "PR_DESCRIPTION")) = $md
'
{
"text": "Deployment started :rocket:",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": ":computer: Deployment release :computer:"
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*${{ steps.date.outputs.date }} | STAGING*"
}
]
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "### TITLE\n\n\n**bold text**\n\n\n- qsqds\n- qsdqds\n- qsdqsd\n- qsdqsd\n\n1. qsdqsd\n2. qsdqsd\n3. qsdqsd\n4. qsdqsd\n\n`some code here`\n"
}
},
{
"type": "divider"
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "🚰 Pipeline"
},
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}
]
}
],
"attachments": [
{
"color": "warning",
"fields": [
{
"title": "Statut",
"short": true,
"value": "Deploying ..."
}
]
}
]
}