In: Computer Science
I want to remove the unnecessary characteristics from json.
bad JSON
"{\n \"response\": {\n \"Id\": \"1234\",\n \"la\": [\n {\n \"op\": \"pic\",\n \"lbl\": \"33609\",\n
I want the json to be like this
{"response":{"Id":"1234","la":[{"op":"pic","lbl":"33609","
I have tried to used URLDecoder.decode(param1AfterEncoding, "UTF-8"); but it didn't fix it. I would apperciate any help
you can use the following program and check if it works........
import json
import sys
from pkgutil import simplegeneric
@simplegeneric
def get_items(obj):
while False: # no items, a scalar object
yield None
@get_items.register(dict)
def _(obj):
return obj.items() # json object.
@get_items.register(list)
def _(obj):
return enumerate(obj) # json array
def strip_whitespace(json_data):
for key, value in get_items(json_data):
if hasattr(value, 'strip'): # json string
json_data[key] = value.strip()
else:
strip_whitespace(value) # call recursively
data = json.load(sys.stdin) # read json data from stdin
strip_whitespace(data)
json.dump(data, sys.stdout, indent=2)
..............................................................................................................
and also you can try this one to remove bakslashes.....
import json
# result = json.dumps(result)
with open('result.json', 'w') as fp:
json.dump(result, fp, indent=4)