How do i get rid of the backward slash in json python?

I am using python url library to get json response from spatial reference website.This is my code. I get response_read="u'{\'type\': \'EPSG\', \'properties\': {\'code\': 102646}}'" but i need this response in this form:"{'type': 'EPSG', 'properties': {'code': 102646}}". How i achieve output in this form?

headers = {'User-Agent': 'Mozilla/5.0'}
 req = urllib2.Request["//spatialreference.org/ref/esri/"nad-1983-stateplane-california-vi-fips-0406-feet"/json/", None, headers]
        response = urllib2.urlopen[req]
        response_read = response.read[].decode['utf-8']
        result = json.dumps[response_read]  
        epsg_json = json.loads[result]
        epsg_code = epsg_json['properties']['code']
        return epsg_code

asked Jul 19, 2017 at 14:18

you need to first use dumps then loads

json_data = json.dumps[response_read]
json_without_slash = json.loads[json_data]

answered Jun 7, 2021 at 8:46

I am not very sure your is a function or not. Anyways, the response you receive is having the literal character ' and you need to replace it with ".

Here is the working code:

import urllib2,json
headers = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request["//spatialreference.org/ref/esri/nad-1983-stateplane-california-vi-fips-0406-feet/json/", None, headers]
response = urllib2.urlopen[req]
response_read = response.read[]
epsg_json = json.loads[response_read.replace["\'", '"']]
epsg_code = epsg_json['properties']['code']
print[epsg_code]

Hope this helps.

answered Jul 19, 2017 at 15:19

user1211user1211

1,4671 gold badge18 silver badges27 bronze badges

1

Replies: 2 suggested answers 2 replies

Add heading textAdd bold text, Add italic text,

Add a quote, Add code,

Insert Link

Add a link,

Add a bulleted list, Add a numbered list, Add a task list,

Directly mention a user or teamReference an issue or pull request

Add heading textAdd bold text, Add italic text, Add a bulleted list, Add a numbered list, Add a task list,

reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji

@AnnuKumari-MSFT
Thank you so much for your feedback and help.
I used your dynamic expression, and it seems to be working much better, but I have one more issue to take care in order to map to SQL table.

This is expression that I used:
@replace[replace[string[activity['Web1'].output.output],'\r\n',''],'\','']
[This pipeline is different from other one, so I have little different expression]

Output of JSON file came as like this:
[" { "GLDETAIL": { "RECORDNO": "249125-1688259-784668--accrual", "BATCH_DATE": "01/01/2022" } }, { "GLDETAIL": { "RECORDNO": "249125-1688260-784669--accrual", "BATCH_DATE": "01/01/2022" } },"]

I am trying to get rid of first " that appears right after [ and " at the end before ] - highlighted in yellow:

How do I go about doing it?

This is details of Web1:

[
"\r\n {\r\n \"GLDETAIL\": {\r\n \"RECORDNO\": \"249125-1688259-784668--accrual\",\r\n \"BATCH_DATE\": \"01/01/2022\"\r\n }\r\n },\r\n {\r\n \"GLDETAIL\": {\r\n \"RECORDNO\": \"249125-1688260-784669--accrual\",\r\n \"BATCH_DATE\": \"01/01/2022\"\r\n }\r\n }\r\n,"
],

This is details of myVariable:
"[\" { \"GLDETAIL\": { \"RECORDNO\": \"249125-1688259-784668--accrual\", \"BATCH_DATE\": \"01/01/2022\" } }, { \"GLDETAIL\": { \"RECORDNO\": \"249125-1688260-784669--accrual\", \"BATCH_DATE\": \"01/01/2022\" } },\"]"

Thanks.


How do I remove a backward slash from a string in Python?

Linked.
how to replace back slash character with empty string in python..
Python: Removing backslashes inside a string..
Using str.replace method changes nothing in the original string..
-2. Replacing backlash [\] in a list..
-2. Python remove backslashes from string..

Why does my JSON have backslash?

Those backslashes are escape characters. They are escaping the special characters inside of the string associated with JSON response. You have to use JSON. parse to parse that JSON string into a JSON object.

How do I change a backslash in Python?

A forward-slash [/] in a Python string can be replaced by a backslash [\] using the String replace[] function, translate[] method, or regular expression[re..
1 Using replace[].
2 Using translate[] function..
3 Using regular expression [re.sub[]].
4 Backslash in Python..

How do you remove a string slash?

Use the String. replace[] method to remove a trailing slash from a string, e.g. str. replace[/\/+$/, ''] . The replace method will remove the trailing slash from the string by replacing it with an empty string.

Chủ Đề