0

Although the following seems to be a valid json string, I am unable to json.load it.

In [33]: mystr="{ 'username': 'Newman Test Executor', 'channel': '#someslackchannel' }"
In [34]: json.loads(mystr)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-34-6f4efa0d20c6> in <module>()
----> 1 json.loads(mystr)
/usr/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
 337 parse_int is None and parse_float is None and
 338 parse_constant is None and object_pairs_hook is None and not kw):
--> 339 return _default_decoder.decode(s)
 340 if cls is None:
 341 cls = JSONDecoder
/usr/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
 362 
 363 """
--> 364 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
 365 end = _w(s, end).end()
 366 if end != len(s):
/usr/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx)
 378 """
 379 try:
--> 380 obj, end = self.scan_once(s, idx)
 381 except StopIteration:
 382 raise ValueError("No JSON object could be decoded")
ValueError: Expecting property name: line 1 column 3 (char 2)
asked Mar 14, 2019 at 18:04
1
  • Rule#1: JSON strings should have double quoted property names. Commented Mar 14, 2019 at 18:12

4 Answers 4

2

As both @Austin and @Arya mentioned above, you need to have double quotes in your JSON string to be valid. In your case, you could simply replace the single quote with a double quote:

import json
mystr="{ 'username': 'Newman Test Executor', 'channel': '#someslackchannel' }"
json.loads(mystr.replace('\'','"'))
answered Mar 14, 2019 at 18:17
Sign up to request clarification or add additional context in comments.

Comments

2
import json
mystr = '{ "username": "Newman Test Executor", "channel": "#someslackchannel" }'
my_dict = { "username": "Newman Test Executor", "channel": "#someslackchannel" }
print(json.loads(mystr))
print(json.dumps(my_dict))

output:

{u'username': u'Newman Test Executor', u'channel': u'#someslackchannel'}
{"username": "Newman Test Executor", "channel": "#someslackchannel"}

Single quotes outside, double quotes inside incase of strings.

If you have no control over the json string being passed to you, you can use mystr.replace('\'', '"') as mentioned by Vasilis G.

answered Mar 14, 2019 at 18:18

Comments

0

You need double quotes inside of the JSON string.

See Parsing string as JSON with single quotes?

The JSON standard requires double quotes and will not accept single quotes, nor will the parser.

answered Mar 14, 2019 at 18:08

Comments

0

Followed what Arya commented and got the code to work. Answer can be found in another forum though..

import json
mystr='{ "username": "Newman Test Executor","channel": "#someslackchannel"}'
json.loads(mystr)
answered Mar 14, 2019 at 18:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.