I'm writing a script in which i take a value out from JSON data and trying to replace "/"
with "\/"
, using the code below:
For example the JSON is:
Note: I'm loading JSON from a file and this is only example.
JSON = {'external_urls': {'source': 'https://someexample.com/etc1'},
'href': 'https://somesource.com/etc2/',
'name': 'John Williams'
}
The little script that I've written is:
import json
from ppprint import pprint
JSON = json.loads(s.content)
oldUrl = JSON['href']
newFomattedUrl = oldUrl.replace('/','\/')
print(newFormattedUrl)
JSON['href'] = newFormattedUrl
print("New JSON Data is:")
pprint(JSON)
Actual format of href that I want to achieve is:
https:\/\/somesource.com\/etc2\/
but the href value after printing to console as well as writing to the file appeared as (in both the cases):
'href': 'https:\\/\\/somesource.com\\/etc2\\/
instead of appearing same as above mentioned format with heading "Actual Format of href".
I also used different posts to get help mostly people said use this to format to replace in replace()
function:
new = orlUrl.replace('/',r'\/')
but no luck.
Also, some people said it is causing because I'm making a print before writing to updating JSON, i also tried by removing print statement which was printing newFormattedUrl but the updated json has an extra back slash and so the newly written json file also.
I'm learning python so sorry if I didn't explain it well or have some grammatical mistakes.
I even tried this:
1. newFomattedUrl = oldUrl.replace('/','\/')
but was getting the same result.