如何在Shell中使用数组更新嵌套JSON中的JSON值

yfwxisqw  于 2023-02-24  发布在  Shell
关注(0)|答案(1)|浏览(247)

我有一个JSON文件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 ..."
            }
         ]
      }
   ]
}

和一个txt文件pr_body.txt

### TITLE

**bold text**

- qsqds
- qsdqds
- qsdqsd
- qsdqsd

1. qsdqsd
2. qsdqsd
3. qsdqsd
4. qsdqsd

`some code here`

我的目标是将payload.json中的PR_DESCRIPTION替换为pr_body.txt中的格式化文本
使用jq是否可以做到这一点?
我尝试使用jq来更新我的JSON,但是由于数组对象,我很难找到正确的方法来完成它。
我当然可以用Python来做,但是使用Shell命令来做对我来说是很有前途的。
先谢谢你!

5rgfhyps

5rgfhyps1#

可以使用-R选项将markdown文件作为文本读入,使用--argfile选项读入JSON文件,然后,使用select将路径为.blocks[].text.text、内容为"PR_DESCRIPTION"的节点设置为markdown输入,该值之前存储在一个变量中:

< 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 ..."
        }
      ]
    }
  ]
}

相关问题