Python Forum

Full Version: script: remove all "carriage return" from my json variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
in my script I have a variable json with its fields (key, value)

I would like to remove all the "CRLF" (or "\n") from all the value fields of the json

Do you know how I can do?

thank you!
can you show sample input and desired output and what have you tried. Also provide information where this json comes from - i.e. I think it's more likely you mishandle the json when creating or parsing it
Hi buran,
your perfectly right, sorry for this missing

my json variables come from a fetch from mysql DB (they are assigned by the json columns of a table)

I tried this, unsuccessfully:

sql = "SELECT json_col1, json_col2 FROM mytable"
cur.execute(sql)
result_set = cur.fetchall()
for row in result_set:

		x_json_col1 = row[0]
		json_test1 = x_json_col1.strip('\n')

		x_json_col2 = row[1]
		json_test2 = x_json_col2.strip('\n')

...
I need to remove all the CRLF from the text: consider that the column values come from free text from a web page, so the user can insert everything, but, after the fetch, I have to insert the column values into another DB and this one does not accept CRLF, accented chars or similar special chars...

hope explained better, but it's a weird issue for me...
print repr(x_json_col1)) to see what you are dealing with. try using just .strip() - it will remove all white spaces, e.g. '\r'
thanks buran, appreciate your help!

tried and works

for everyone, for something more particular I also tried and test this:

	x_json = row[1]
		loaded_json = json.loads(x_json)
		
		for each in loaded_json["items"]:
			if loaded_json["items"][each]["name"] is None:
				curdata = ""
			else:
				curdata = loaded_json["items"][each]["name"].replace("\n","")
			
			loaded_json["items"][each]["name"] = curdata
		
		myjson=json.dumps(loaded_json)
see the next!