Python Requests Json Returns Single Quote

python requests json returns single quote

The json() method doesn't actually return JSON. It returns a python object (read: dictionary) that contains the same information as the json data. When you print it out, the quotes are added for the sake of readability, they are not actually in your data.

Should I care about it or not?

Not.

I can't find a way to extract data from single quoted JSON in Python

I think you misunderstand: req = r.json(). This has already decoded the json response for you and converted it to a python dict.

Did you mean: print(req['internalipaddress'])?

Just to be clear about the types involved:

import json

j = '{"id": "REDACTED", "internalipaddress": "REDACTED"}'

print(type(j), j)

d = json.loads(j)

print(type(d), d)

Output:

<class 'str'> {"id": "REDACTED", "internalipaddress": "REDACTED"}
<class 'dict'> {'id': 'REDACTED', 'internalipaddress': 'REDACTED'}

The variable j is a string whose contents match the json specification which means that the json module can be used to convert the string into a python dict.

When each are printed, the string has to have double quotes around strings, but when python prints the contents of the dict the strings there are rendered as being surrounded with single quotes.

python request library giving wrong value single quotes

What you see here ("Bachelor\u2019s Degree") is the string's inner representation, where "\u2019" is the unicode codepoint for "RIGHT SINGLE QUOTATION MARK". This is perfectly correct, there's nothing wrong here, if you print() this string you'll get what you expect:

>>> s = 'Bachelor\u2019s Degree'
>>> print(s)
Bachelor’s Degree

Learning about unicode and encodings might save you quite some time FWIW.

EDIT:

When I save in db and then on displaying on HTML it will cause issue
right?

Have you tried ?

Your database connector is supposed to encode it to the proper encoding (according to your fields, tables and client encoding settings).

wrt/ "displaying it on HTML", it mostly depends on whether you're using Python 2.7.x or Python 3.x AND on how you build your HTML, but if you're using some decent framework with a decent template engine (if not you should reconsider your stack) chances are it will work out of the box.

As I already mentionned, learning about unicode and encodings will save you a lot of time.

Single vs double quotes in JSON

JSON syntax is not Python syntax. JSON requires double quotes for its strings.

Replace single quotes in a string but not escaped single quotes

You need a negative lookbehind, not a negative lookahead ("no backslash before a quote"):

result = '''{'key1': 4, 'key2': 'I\\'m home'}'''
print(re.sub(r"(?<!\\)'", '"', result))
#{"key1": 4, "key2": "I\'m home"}

Python equivalent of wrapping POST payload in single quotes

Well, "multiple json objects" is not a valid json, until it's a list of objects.

Generally, python doesn't care (just like any other network tool), json is just data format, and you need a different one. So you need to construct text payload yourself, i.e. json.dumps(payload1) + json.dumps(payload2), and send it via your network client as "raw" data.

I highly doubt that mainstream http libraries provide such usecase out of the box.


Not sure on reason for downvotes, i.e. requests library (which is kinda standard de-facto for high-level networking) have smart processing for payloads:

requests.post(url, data={'v1': 1, 'v2': 2})  # will encode it as form data
requests.post(url, json={'v1': 1, 'v2': 2}) # will encode as json
requests.post(url, data="{'v1': 1}{'v2': 2}") # will send as-is

Json has nothing to do with http itself, it's just a way to serialize data. Most clients will eventually use urllib, which doesn't care at all, the only question is if library gives easy way to send data raw

Python json difference between single quotes and double quotes

RFC7159 defining JSON stipulates

A string begins and ends with quotation marks.

i.e. ", it does not allow ' unlike python

Handling Single Quote in JSON using Python & Snowflake

Use parameter binding so that the quoting is handled automatically.

from sqlalchemy import text 
...
results = connection.execute(
text("INSERT INTO db_name.schema_name.sqlalchemy(jval) (select PARSE_JSON(:some_json))"),
{'some_json': jval}
).fetchone()

JSON string Replace single quote with two single quotes in Python

Shouldn't all of this:

sql_declare = "DECLARE @json NVARCHAR(MAX) = N'" + json_str + "'"
sql_stat = "INSERT ... FROM OPENJSON(@json, '$.results') ..."
sql = sql_declare + " " + sql_stat
cursor.execute(sql)

Just be:

sql = "INSERT ... FROM OPENJSON(?, '$.results') ...);"
cursor.execute(sql, json_string)

Now you're passing in a parameter and don't have to deal with local variables in T-SQL, string delimiters, single quotes, etc.



Related Topics



Leave a reply



Submit