删除 json 而非 json 描述中的空格、换行符和制表符

Remove whitespaces, newlines and tabs in json not in json descriptions

我有一个 json 文件:

{
  "parameters": [
    {
      "productGroup": "-1",
      "proofIncome": "-1",
      "docSignType": "-1",
      "creditSumm": "StatusConfirmAmount",
      "message": "Abbreviated verification parameters"
    },
    {
      "productGroup": "-1",
      "proofIncome": "-1",
      "docSignType": "-1",
      "creditSumm": "AdditionalDocumentAmount",
      "message": "Abbreviated validation parameters.\r\n\r\n You need to check the 2nd document\r\n 1 page of the contract is enough"
    }
  ]
}

如何删除 json 中的空格、换行符和制表符,而不是 json "message": 描述中的 json 格式化程序 - minify/compact:

使用 Powershell 或 Python?

PowerShell 的 ConvertTo-Json cmdlet 有一个 -Compress 参数:

@'
{
  "parameters": [
    {
      "productGroup": "-1",
      "proofIncome": "-1",
      "docSignType": "-1",
      "creditSumm": "StatusConfirmAmount",
      "message": "Abbreviated verification parameters"
    },
    {
      "productGroup": "-1",
      "proofIncome": "-1",
      "docSignType": "-1",
      "creditSumm": "AdditionalDocumentAmount",
      "message": "Abbreviated validation parameters.\r\n\r\n You need to check the 2nd document\r\n 1 page of the contract is enough"
    }
  ]
}
'@ |ConvertFrom-Json |ConvertTo-Json -Compress

结果:

{"parameters":[{"productGroup":"-1","proofIncome":"-1","docSignType":"-1","creditSumm":"StatusConfirmAmount","message":"Abbreviated verification parameters"},{"productGroup":"-1","proofIncome":"-1","docSignType":"-1","creditSumm":"AdditionalDocumentAmount","message":"Abbreviated validation parameters.\r\n\r\n You need to check the 2nd document\r\n 1 page of the contract is enough"}]}

在 python 中,您可以通过 缩小 json:

import json

data = """
{
  "parameters": [
    {
      "productGroup": "-1",
      "proofIncome": "-1",
      "docSignType": "-1",
      "creditSumm": "StatusConfirmAmount",
      "message": "Abbreviated verification parameters"
    },
    {
      "productGroup": "-1",
      "proofIncome": "-1",
      "docSignType": "-1",
      "creditSumm": "AdditionalDocumentAmount",
      "message": "Abbreviated validation parameters.\r\n\r\n You need to check the 2nd document\r\n 1 page of the contract is enough"
    }
  ]
}
"""

minified = json.dumps(json.loads(data), separators=(',', ':'))

print(minified)

结果:

{"parameters":[{"productGroup":"-1","proofIncome":"-1","docSignType":"-1","creditSumm":"StatusConfirmAmount","message":"Abbreviated verification parameters"},{"productGroup":"-1","proofIncome":"-1","docSignType":"-1","creditSumm":"AdditionalDocumentAmount","message":"Abbreviated validation parameters.\r\n\r\n You need to check the 2nd document\r\n 1 page of the contract is enough"}]}