Search code examples
pythonjsonpowershellremoving-whitespace

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


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?


Solution

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