Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expecting property name enclosed in double quotes #5412

Closed
jeffreyscottgraham opened this issue Apr 3, 2020 · 9 comments
Closed

Expecting property name enclosed in double quotes #5412

jeffreyscottgraham opened this issue Apr 3, 2020 · 9 comments

Comments

@jeffreyscottgraham
Copy link

Summary.
Cant parse json result because property names are in single quotes instead of double

Expected Result

A proper parse of course.

Actual Result

Caught exception Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Reproduction Steps

res=requests.get(...)
s=str(res.json())
j=json.loads(s)

System Information

python3 -m requests.help
{
"chardet": {
"version": "3.0.4"
},
"cryptography": {
"version": "2.3"
},
"idna": {
"version": "2.6"
},
"implementation": {
"name": "CPython",
"version": "3.7.3"
},
"platform": {
"release": "5.0.0-38-generic",
"system": "Linux"
},
"pyOpenSSL": {
"openssl_version": "1010100f",
"version": "19.0.0"
},
"requests": {
"version": "2.21.0"
},
"system_ssl": {
"version": "1010102f"
},
"urllib3": {
"version": "1.24.1"
},
"using_pyopenssl": true
}

This is running on up-to-date Ubuntu 19.04 and python 3.7.3

@nateprewitt
Copy link
Member

Hi @jeffreyscottgraham,

This looks like a bit of confusion about data types. You're attempting to cast a Python dictionary into a string which will use single quotes by convention.

Python dictionaries aren't directly equivalent with JSON. If you need a string representation of the data, you can either call text() on your response, or use json.dumps instead of trying to cast as a string.

@jeffreyscottgraham
Copy link
Author

I did finally figure out to use text().... i spent 4 hours literally hacking around for a solution.
May I request that an example be made part of the regular readme/documentation around the useof json(). It would have prevented all the wasted time looking for the solution.
Thanks for the ironically quick response :)

@nateprewitt
Copy link
Member

Do you have any suggestions on what you'd like to see added to the docs? We currently have example usage on the front page and a more detailed section in the quick start guide.

@sigmavirus24
Copy link
Contributor

@jeffreyscottgraham I don't think it's up to the documentation of this project to teach you how to write Python.

@nateprewitt
Copy link
Member

Closing since the documentation appears to be in place and we didn't receive further feedback.

@osamaramihafez
Copy link

@sigmavirus24 examples actually speed up the development process. "Tell me and I forget, teach me and I learn".

@sigmavirus24
Copy link
Contributor

@osamaramihafez we have examples but no one has explained how to improve them or what was confusing. OP here seemed to not find an example that's in the docs but didn't tell us what they looked for or how to improve that discoverability. Without details beyond "I did this wrong and your docs are bad" we can't action very much. That said, every library's documentation can not be responsible for making sure you've learned the language sufficiently well by teaching you how to write code in that language. There will always be superior resources for that elsewhere and it's not worth my time to half-ass that. Finally, more is not always better of it makes crucial examples harder to find. No piece of documentation written entirely by volunteers on limited time can satisfy everyone, but it can satisfy 95% and that's better than perfect.

@EightRice
Copy link

Hi @jeffreyscottgraham,

This looks like a bit of confusion about data types. You're attempting to cast a Python dictionary into a string which will use single quotes by convention.

Python dictionaries aren't directly equivalent with JSON. If you need a string representation of the data, you can either call text() on your response, or use json.dumps instead of trying to cast as a string.

was this thread supposed to help me convert "{'some string': 140}" into a json?

You're suggesting to use json.dumps, but if I could do that I wouldn't need to convert it INTO a json. That's how I'm getting the strings and I don't have control over how they get encoded.

@sigmavirus24
Copy link
Contributor

@EightRice if you can't import something from the standard library I don't know how to help you generate valid JSON or handle invalid JSON.

Also, if you're getting garbage over the wire, it's not this library's responsibility to magically understand the intent you have for using garbage. You'll need to discern how to parse that string correctly and (hopefully) safely and understand those trade-offs because, as I think you realize, you're not being sent JSON.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants