编码 JSON 值,同时禁用换行

Encode JSON value while disabling line wrapping

我最初遇到的问题是 JSON(我们称它为“raw_data.json”),看起来像这样:

[
  {
    "metadata": {
      "project": [
        "someProject1",
        "someProject2"
      ],
      "table_name": [
        "someTable1",
        "someTable2"
      ],
      "sys_insertdatetime": "someDate",
      "sys_sourcesystem": "someSourceSystem"
    },
    "data": {
      "field1": 63533712,
      "field2": "",
      "field3": "hello",
      "field4": "other",
      "field5": 2022,
      "field6": "0",
      "field7": "0",
      "field8": "0",
      "field9": "0",
      "field10": "0",
      "field11": "0"
    }
  },
  {
    "metadata": {
      "project": [
        "someProject1",
        "someProject2"
      ],
      "table_name": [
        "someTable1",
        "someTable2"
      ],
      "sys_insertdatetime": "someDate",
      "sys_sourcesystem": "someSourceSystem"
    },
    "data": {
      "field1": 63533713,
      "field2": "Y2JjLTIwMjItdzA1LXRyZi1vZmZyZXMtcmVuZm9ydC1jaGVxdWllci13MDU=",
      "field3": "A0AVB",
      "field4": "other",
      "field5": "HJlbmZvcnQgY2hlcXVpZXIgVzA1",
      "field6": "",
      "field7": "02/02/2022",
      "field8": "14/02/2022",
      "field9": "Ticket"
    }
  }
]

这些词典非常大,通常在我的显示器上跨越几行。这是一个问题,因为我需要在 Base64 中对整个内容进行编码并使用 HTTP POST 方法发送结果。但是编码倾向于通过添加换行符来换行,这导致我的 POST 方法失败。

我幸运地找到了一个question解决方案如下:

export DATA=$(cat 'raw_data.json')
export ENCODED_DATA=$(echo "$DATA" | jq -c . | base64 -w 0)

问题是,我的JSON现在变成了这样:

{
  "request_id": 1234,
  "data_to_be_encoded": [
    {
      "metadata": {
        "project": [
          "someProject1",
          "someProject2"
        ],
        "table_name": [
          "someTable1",
          "someTable2"
        ],
        "sys_insertdatetime": "someDate",
        "sys_sourcesystem": "someSourceSystem"
      },
      "data": {
        "field1": 63533712,
        "field2": "",
        "field3": "hello",
        "field4": "other",
        "field5": 2022,
        "field6": "0",
        "field7": "0",
        "field8": "0",
        "field9": "0",
        "field10": "0",
        "field11": "0"
      }
    },
    {
      "metadata": {
        "project": [
          "someProject1",
          "someProject2"
        ],
        "table_name": [
          "someTable1",
          "someTable2"
        ],
        "sys_insertdatetime": "someDate",
        "sys_sourcesystem": "someSourceSystem"
      },
      "data": {
        "field1": 63533713,
        "field2": "Y2JjLTIwMjItdzA1LXRyZi1vZmZyZXMtcmVuZm9ydC1jaGVxdWllci13MDU=",
        "field3": "A0AVB",
        "field4": "other",
        "field5": "HJlbmZvcnQgY2hlcXVpZXIgVzA1",
        "field6": "",
        "field7": "02/02/2022",
        "field8": "14/02/2022",
        "field9": "Ticket"
      }
    }
  ]
}

基本上我需要保持 request_id 键值对不变,而 data_to_be_encoded 必须对密钥进行编码。同样,另一个 post 提供了一个很好的解决方案:

export DATA=$(cat 'raw_data.json')
export ENCODED_DATA=$(echo "$DATA" | jq '.data_to_be_encoded |= @base64')

除了这个解决方案添加了换行这一事实,我还没有找到禁用该功能的方法,就像我在第一个解决方案中设法做到的那样。

我试过这个:

export ENCODED_DATA=$(echo "$DATA" | jq -c .data_to_be_encoded | base64 -w 0)

但它只是 returns data_to_be_encoded 键内的值,而不是整个 JSON。所以我回到第一个广场。

我怎样才能两全其美?换句话说,如何在编码 JSON?

的特定部分的同时禁用换行

使用 jq 的 @base64 内置函数,而不是在外部进行转换,因为那样你可以全部转换或不转换。

jq '.data_to_be_encoded |= @base64'
{
  "request_id": 1234,
  "data_to_be_encoded": "W3sibWV0YWRhdGEiOnsicHJvamVjdCI6WyJzb21lUHJvamVjdDEiLCJzb21lUHJvamVjdDIiXSwidGFibGVfbmFtZSI6WyJzb21lVGFibGUxIiwic29tZVRhYmxlMiJdLCJzeXNfaW5zZXJ0ZGF0ZXRpbWUiOiJzb21lRGF0ZSIsInN5c19zb3VyY2VzeXN0ZW0iOiJzb21lU291cmNlU3lzdGVtIn0sImRhdGEiOnsiZmllbGQxIjo2MzUzMzcxMiwiZmllbGQyIjoiIiwiZmllbGQzIjoiaGVsbG8iLCJmaWVsZDQiOiJvdGhlciIsImZpZWxkNSI6MjAyMiwiZmllbGQ2IjoiMCIsImZpZWxkNyI6IjAiLCJmaWVsZDgiOiIwIiwiZmllbGQ5IjoiMCIsImZpZWxkMTAiOiIwIiwiZmllbGQxMSI6IjAifX0seyJtZXRhZGF0YSI6eyJwcm9qZWN0IjpbInNvbWVQcm9qZWN0MSIsInNvbWVQcm9qZWN0MiJdLCJ0YWJsZV9uYW1lIjpbInNvbWVUYWJsZTEiLCJzb21lVGFibGUyIl0sInN5c19pbnNlcnRkYXRldGltZSI6InNvbWVEYXRlIiwic3lzX3NvdXJjZXN5c3RlbSI6InNvbWVTb3VyY2VTeXN0ZW0ifSwiZGF0YSI6eyJmaWVsZDEiOjYzNTMzNzEzLCJmaWVsZDIiOiJZMkpqTFRJd01qSXRkekExTFhSeVppMXZabVp5WlhNdGNtVnVabTl5ZEMxamFHVnhkV2xsY2kxM01EVT0iLCJmaWVsZDMiOiJBMEFWQiIsImZpZWxkNCI6Im90aGVyIiwiZmllbGQ1IjoiSEpsYm1admNuUWdZMmhsY1hWcFpYSWdWekExIiwiZmllbGQ2IjoiIiwiZmllbGQ3IjoiMDIvMDIvMjAyMiIsImZpZWxkOCI6IjE0LzAyLzIwMjIiLCJmaWVsZDkiOiJUaWNrZXQifX1d"
}

Demo

如果您需要全部在一行中,请像以前一样使用 -c 选项:

jq -c '.data_to_be_encoded |= @base64'
{"request_id":1234,"data_to_be_encoded":"W3sibWV0YWRhdGEiOnsicHJvamVjdCI6WyJzb21lUHJvamVjdDEiLCJzb21lUHJvamVjdDIiXSwidGFibGVfbmFtZSI6WyJzb21lVGFibGUxIiwic29tZVRhYmxlMiJdLCJzeXNfaW5zZXJ0ZGF0ZXRpbWUiOiJzb21lRGF0ZSIsInN5c19zb3VyY2VzeXN0ZW0iOiJzb21lU291cmNlU3lzdGVtIn0sImRhdGEiOnsiZmllbGQxIjo2MzUzMzcxMiwiZmllbGQyIjoiIiwiZmllbGQzIjoiaGVsbG8iLCJmaWVsZDQiOiJvdGhlciIsImZpZWxkNSI6MjAyMiwiZmllbGQ2IjoiMCIsImZpZWxkNyI6IjAiLCJmaWVsZDgiOiIwIiwiZmllbGQ5IjoiMCIsImZpZWxkMTAiOiIwIiwiZmllbGQxMSI6IjAifX0seyJtZXRhZGF0YSI6eyJwcm9qZWN0IjpbInNvbWVQcm9qZWN0MSIsInNvbWVQcm9qZWN0MiJdLCJ0YWJsZV9uYW1lIjpbInNvbWVUYWJsZTEiLCJzb21lVGFibGUyIl0sInN5c19pbnNlcnRkYXRldGltZSI6InNvbWVEYXRlIiwic3lzX3NvdXJjZXN5c3RlbSI6InNvbWVTb3VyY2VTeXN0ZW0ifSwiZGF0YSI6eyJmaWVsZDEiOjYzNTMzNzEzLCJmaWVsZDIiOiJZMkpqTFRJd01qSXRkekExTFhSeVppMXZabVp5WlhNdGNtVnVabTl5ZEMxamFHVnhkV2xsY2kxM01EVT0iLCJmaWVsZDMiOiJBMEFWQiIsImZpZWxkNCI6Im90aGVyIiwiZmllbGQ1IjoiSEpsYm1admNuUWdZMmhsY1hWcFpYSWdWekExIiwiZmllbGQ2IjoiIiwiZmllbGQ3IjoiMDIvMDIvMjAyMiIsImZpZWxkOCI6IjE0LzAyLzIwMjIiLCJmaWVsZDkiOiJUaWNrZXQifX1d"}

Demo