-2

I have a json file:

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

How can I remove whitespaces, newlines and tabs in json, but not in json "message": descriptions like json formatter - minify/compact:

json formatter

using Powershell or Python?

technikfe
  • 121
  • 1
  • 8
Denis Mozhaev
  • 59
  • 1
  • 1
  • 6
  • Does this answer your question? [Minify JSON with PowerShell?](https://stackoverflow.com/questions/42082289/minify-json-with-powershell) – Shivam Jan 03 '23 at 23:07

1 Answers1

3

PowerShell's ConvertTo-Json cmdlet has a -Compress parameter for this:

@'
{
  "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

Result:

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

In python you can minify the json by specifying custom separators when calling json.dumps():

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)

Result:

{"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"}]}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206