I have a json file:
JavaScript
x
19
19
1
{
2
"parameters": [
3
{
4
"productGroup": "-1",
5
"proofIncome": "-1",
6
"docSignType": "-1",
7
"creditSumm": "StatusConfirmAmount",
8
"message": "Abbreviated verification parameters"
9
},
10
{
11
"productGroup": "-1",
12
"proofIncome": "-1",
13
"docSignType": "-1",
14
"creditSumm": "AdditionalDocumentAmount",
15
"message": "Abbreviated validation parameters.rnrn You need to check the 2nd documentrn 1 page of the contract is enough"
16
}
17
]
18
}
19
How can I remove whitespaces, newlines and tabs in json, but not in json "message":
descriptions like json formatter – minify/compact:
using Powershell or Python?
Advertisement
Answer
PowerShell’s ConvertTo-Json
cmdlet has a -Compress
parameter for this:
JavaScript
1
21
21
1
@'
2
{
3
"parameters": [
4
{
5
"productGroup": "-1",
6
"proofIncome": "-1",
7
"docSignType": "-1",
8
"creditSumm": "StatusConfirmAmount",
9
"message": "Abbreviated verification parameters"
10
},
11
{
12
"productGroup": "-1",
13
"proofIncome": "-1",
14
"docSignType": "-1",
15
"creditSumm": "AdditionalDocumentAmount",
16
"message": "Abbreviated validation parameters.rnrn You need to check the 2nd documentrn 1 page of the contract is enough"
17
}
18
]
19
}
20
'@ |ConvertFrom-Json |ConvertTo-Json -Compress
21
Result:
JavaScript
1
2
1
{"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.rnrn You need to check the 2nd documentrn 1 page of the contract is enough"}]}
2
In python you can minify the json by specifying custom separators
when calling json.dumps()
:
JavaScript
1
27
27
1
import json
2
3
data = """
4
{
5
"parameters": [
6
{
7
"productGroup": "-1",
8
"proofIncome": "-1",
9
"docSignType": "-1",
10
"creditSumm": "StatusConfirmAmount",
11
"message": "Abbreviated verification parameters"
12
},
13
{
14
"productGroup": "-1",
15
"proofIncome": "-1",
16
"docSignType": "-1",
17
"creditSumm": "AdditionalDocumentAmount",
18
"message": "Abbreviated validation parameters.\r\n\r\n You need to check the 2nd document\r\n 1 page of the contract is enough"
19
}
20
]
21
}
22
"""
23
24
minified = json.dumps(json.loads(data), separators=(',', ':'))
25
26
print(minified)
27
Result:
JavaScript
1
2
1
{"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.rnrn You need to check the 2nd documentrn 1 page of the contract is enough"}]}
2