1

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.

GigaByte
  • 700
  • 6
  • 21
  • 2
    You are confusing the representation for the value; there are no double backslashes in the value. See the duplicate. – Martijn Pieters Nov 07 '18 at 17:37
  • sorry i do not get it sir. – GigaByte Nov 07 '18 at 17:41
  • you also marked it duplicate question. Can you please send me a reference link where this question has been asked? – GigaByte Nov 07 '18 at 17:42
  • sorry get it. :) – GigaByte Nov 07 '18 at 17:43
  • i know about use of escape sequences. My question is that i'm not appending any extra backslash in newFomatted JSon value but why i'm getting extra one when i dump the updated json to my file? – GigaByte Nov 07 '18 at 17:46
  • `'href': 'https:\\/\\/somesource.com\\/etc2\\/` is Python syntax, not JSON. If you see this in a file, you used `str(obj)` or `repr(obj)` to create the file contents. Use `json.dump()` or `json.dumps()` to create JSON. – Martijn Pieters Nov 07 '18 at 17:49
  • `pprint()` is also not a way to produce JSON, if you are trying to use the output of that function. – Martijn Pieters Nov 07 '18 at 17:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183250/discussion-between-gigabyte-and-martijn-pieters). – GigaByte Nov 07 '18 at 17:51
  • And note that JSON **also** uses backslashes to escape backslashes, so `\\` in a JSON file is normal too. – Martijn Pieters Nov 07 '18 at 17:51
  • i get your point that it will be normal case in either python string or json format. Many thanks sir. – GigaByte Nov 07 '18 at 17:58

0 Answers0