import json
import sys
from collections import MutableMapping, MutableSequence
def strip_whitespace(json_data):
if isinstance(json_data, MutableMapping): # json object
it = json_data.items()
elif isinstance(json_data, MutableSequence): # json array
it = enumerate(json_data)
else: # scalar data
return # do nothing
for k, v in it:
if hasattr(v, 'strip'): # json string
json_data[k] = v.strip()
else:
strip_whitespace(v) # recursive call
data = json.load(sys.stdin) # read json from stdin
strip_whitespace(data)
json.dump(data, sys.stdout, indent=2)
aW1wb3J0IGpzb24KaW1wb3J0IHN5cwpmcm9tIGNvbGxlY3Rpb25zIGltcG9ydCBNdXRhYmxlTWFwcGluZywgTXV0YWJsZVNlcXVlbmNlCgpkZWYgc3RyaXBfd2hpdGVzcGFjZShqc29uX2RhdGEpOgogICAgaWYgaXNpbnN0YW5jZShqc29uX2RhdGEsIE11dGFibGVNYXBwaW5nKTogIyBqc29uIG9iamVjdAogICAgICAgIGl0ID0ganNvbl9kYXRhLml0ZW1zKCkKICAgIGVsaWYgaXNpbnN0YW5jZShqc29uX2RhdGEsIE11dGFibGVTZXF1ZW5jZSk6ICMganNvbiBhcnJheQogICAgICAgIGl0ID0gZW51bWVyYXRlKGpzb25fZGF0YSkKICAgIGVsc2U6ICMgc2NhbGFyIGRhdGEKICAgICAgICByZXR1cm4gIyBkbyBub3RoaW5nCgogICAgZm9yIGssIHYgaW4gaXQ6CiAgICAgICAgaWYgaGFzYXR0cih2LCAnc3RyaXAnKTogIyBqc29uIHN0cmluZwogICAgICAgICAgICBqc29uX2RhdGFba10gPSB2LnN0cmlwKCkKICAgICAgICBlbHNlOgogICAgICAgICAgICBzdHJpcF93aGl0ZXNwYWNlKHYpICMgcmVjdXJzaXZlIGNhbGwKCgpkYXRhID0ganNvbi5sb2FkKHN5cy5zdGRpbikgIyByZWFkIGpzb24gZnJvbSBzdGRpbgpzdHJpcF93aGl0ZXNwYWNlKGRhdGEpCmpzb24uZHVtcChkYXRhLCBzeXMuc3Rkb3V0LCBpbmRlbnQ9Mik=
ewogICAgJnF1b3Q7bmFtZSZxdW90OzpbCiAgICAgICAgewogICAgICAgICAgICAmcXVvdDtzb21lS2V5JnF1b3Q7OiAmcXVvdDtcblxuICAgc29tZSBWYWx1ZSAgICZxdW90OwogICAgICAgIH0sCiAgICAgICAgewogICAgICAgICAgICAmcXVvdDtzb21lS2V5JnF1b3Q7OiAmcXVvdDthbm90aGVyIHZhbHVlICAgICZxdW90OwogICAgICAgIH0KICAgIF0sCiAgICAmcXVvdDthbm90aGVyTmFtZSZxdW90OzpbCiAgICAgICAgewogICAgICAgICAgICAmcXVvdDthbkFycmF5JnF1b3Q7OiBbCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgJnF1b3Q7a2V5JnF1b3Q7OiAmcXVvdDsgICAgdmFsdWVcblxuJnF1b3Q7LAogICAgICAgICAgICAgICAgICAgICZxdW90O2Fub3RoZXJLZXkmcXVvdDs6ICZxdW90OyAgdmFsdWUmcXVvdDsKICAgICAgICAgICAgICAgIH0sCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgJnF1b3Q7a2V5JnF1b3Q7OiAmcXVvdDsgICAgdmFsdWVcbiZxdW90OywKICAgICAgICAgICAgICAgICAgICAmcXVvdDthbm90aGVyS2V5JnF1b3Q7OiAmcXVvdDt2YWx1ZSZxdW90OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICBdCiAgICAgICAgfQogICAgXQp9
{
"name":[
{
"someKey": "\n\n some Value "
},
{
"someKey": "another value "
}
],
"anotherName":[
{
"anArray": [
{
"key": " value\n\n",
"anotherKey": " value"
},
{
"key": " value\n",
"anotherKey": "value"
}
]
}
]
}