Python Forum
script: remove all "carriage return" from my json variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
script: remove all "carriage return" from my json variable
#1
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!
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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...
Reply
#4
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'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 2,062 Aug-13-2023, 12:16 AM
Last Post: cubangt
  Trouble with threading and reading variable from a different script Lembas 14 3,047 Apr-26-2023, 11:21 PM
Last Post: Lembas
  Python Script to convert Json to CSV file chvsnarayana 8 2,519 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Remove a space between a string and variable in print sie 5 1,784 Jul-27-2022, 02:36 PM
Last Post: deanhystad
Question How can I import a variable from another script without executing it ThomasFab 12 7,815 May-06-2022, 03:21 PM
Last Post: bowlofred
  How to return the next page from json recursively? sandson 0 1,153 Apr-01-2022, 11:01 PM
Last Post: sandson
  Getting Shell to take prompt string plus int value and carriage return bayouprophet 1 1,753 Aug-29-2020, 05:13 PM
Last Post: bowlofred
  Function will not return variable that I think is defined Oldman45 6 3,517 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  Use dynamic variable from parallel running python script Sicksym 0 1,847 May-15-2020, 02:52 PM
Last Post: Sicksym
  [split] script: remove all "carriage return" from my json variable pete 2 2,801 May-05-2020, 03:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020