Python Json.decoder.JSONDecodeError: Expecting ',' delimiter: - python

I'm trying to load a dictionary into json but I'm getting error.
strsdc = '''
{"ext":{"amznregion":["useast"],"someURL":"https://som_url.com/x/px/Iyntynt/{"c":"client","als2s":TIME}","id":"7788y"}}
'''
json.loads(strsdc)
gives me the following error:
Traceback (most recent call last):
File "main.py", line 187, in
lol = json.loads(str(strsdc))
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/init.py", line 357, in loads
return _default_decoder.decode(s)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 2 column 79 (char 79)

Your json string is invalid.
I don't know if this is the way you want it but you can try using this string
{"ext":{"amznregion":["useast"],"someURL":"https://som_url.com/x/px/Iyntynt/{\"c\":\"client\",\"als2s\":TIME}","id":"7788y"}}

Related

json.decoder.JSONDecodeError: Expecting value: line 1 column 18 (char 17) with not finding any fault

Maybe you know a bit more than I about those Decode Errors, but I got this error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Try Me Btch\AppData\Local\Programs\Python\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:\Users\FlexGames\Desktop\Programming\Discord Bot\New Bot\main.commands.py", line 91, in on_message
await XPRole.save_last_msg(fb)
File "C:\Users\FlexGames\Desktop\Programming\Discord Bot\New Bot\New_Functions\XPRole.py", line 59, in save_last_msg
jsonhandle.update("last_message", lastmsg, "lastmsg")
File "C:\Users\FlexGames\Desktop\Programming\Discord Bot\New Bot\Functions\jsonhandle.py", line 56, in update
_load(jsonfile)
File "C:\Users\FlexGames\Desktop\Programming\Discord Bot\New Bot\Functions\jsonhandle.py", line 73, in _load
lmsgjson = json.load(lmsgload)
File "C:\Users\Try Me Btch\AppData\Local\Programs\Python\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\Try Me Btch\AppData\Local\Programs\Python\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Try Me Btch\AppData\Local\Programs\Python\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Try Me Btch\AppData\Local\Programs\Python\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 18 (char 17)
My code where the error is:
elif jsonfile == "lastmsg":
global lmsgjson
with open("C:/Users/FlexGames/Desktop/Programming/Discord Bot/New Bot/lastmsg.json", "r") as lmsgload:
lmsgjson = json.load(lmsgload)
I have no clue what it could be as I also tried to remove the complete path so only going with "lastmsg.json" but I got the same error.
Solution:
You cannot do your file like
{"whatever": }

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) while opening json file

i am trying to create blog page using flask and i want to take input file as a config.json which i have created. Please help me i am getting json decoder error
i also tried to convert str to utf8 but its showing the error
with open('config.json', 'r', encoding ='utf-8') as c:
params = json.load(c)["params"]
json content:
{
"params":
{
"local_server":"True",
"local_uri":"mysql://root:#localhost/codingthunder",
"prod_uri":"mysql://root:#localhost/codingthunder",
"fb_url":"https://facebook.com/codingthunder",
"tw_url":"https://twitter.com/codingthunder",
"gh_url":"https://github.com/codingthunder",
"blog_name":"Coding Thunder",
"tag_line":"A Blog liked by Programmers"
}
}
output log:
PS C:\Users\ASHISH\Desktop\Coding\Flask> python -u "c:\Users\ASHISH\Desktop\Coding\Flask\Blog Page\main.py"
Traceback (most recent call last):
File "c:\Users\ASHISH\Desktop\Coding\Flask\Blog Page\main.py", line 7, in <module>
params = json.load(c)["params"]
File "C:\Users\ASHISH\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\ASHISH\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\ASHISH\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\ASHISH\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Expecting value: line 1 column 1 (char 0) hints that there is no value at the beginning of the file, so no content in the parsed file.
This could mean, that
the file is empty
you opened the wrong or a non existing file
you are using a relative path and your working direktory is wrong (e.g. the direktory you execute your program in)

"JSONDecodeError: Invalid \escape" while trying to load JSON into Python dict

I am trying to load JSON into a python dict:
>>> d
'[{"amount":"0","categories":["test","test2","test3"],"categoryIds":["A001G001","A003E001A001","A003G002A001","A003T001A001"]}]'
Unfortunatelly I do get an error message which I could not get around:
>>> json.loads(d, strict=False)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 361, in loads
return cls(**kw).decode(s)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid \escape: line 1 column 410 (char 409)
How can I load the JSON into a python dictionary?
There is an object within the json:
{"amount":"0","categories":["Gereizte Haut","Gesichtspflege f\xFCr allergische Haut","Insektenstiche","Sonnenallergie & Mallorca-Akne","Basispflege & Reinigung"],"categoryIds":["A001G002","HN001A002G001","HN001A002I002","HN001A002S001","HN001N001B001"],"name":"Linola akut 0,5% Creme","price":969,"pzn":"02138990"}
with the string: "Gesichtspflege f\xFCr allergische Haut" which is not valid json.
You need to get your source to encode the data properly.

How to convert this json string to dict?

After executing the following code:
import json
a = '{"excludeTypes":"*.exe;~\\$*.*"}'
json.loads(a)
I get:
Traceback (most recent call last):
File "", line 1, in
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
So how can I convert 'a' to dict.
Please note that the string is already in 'a' and I cannot add 'r' in front of it. Ideally, the string should have been {"excludeTypes":"*.exe;~\\\\$*.*"}
Also, the following code doesn't work:
import json
a = '{"excludeTypes":"*.exe;~\\$*.*"}'
b = repr(a)
json.loads(b)
import ast
d = ast.literal_eval(a)
By escaping Escape Character "\":
import json
a = '{"excludeTypes":"*.exe;~\\$*.*"}'
a = a.replace("\\","\\\\")
json.loads(a)

json module bug in Python 3.4.1?

I'm trying to use SharePoint 2010's REST API, which was going swimmingly until i ran into this:
Traceback (most recent call last):
File "TestJSON.py", line 21, in <module>
json.loads(s)
File "c:\Python33\lib\json\__init__.py", line 316, in loads
return _default_decoder.decode(s)
File "c:\Python33\lib\json\decoder.py", line 351, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\Python33\lib\json\decoder.py", line 367, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 14 (char 13)
Test case:
import json
s='''{"etag": "W/\"1\""}'''
json.loads(s)
Python 3.3.5 gives the same error. Did i find a bug in the JSON library?
Update:
The actual error i'm getting (preceded by affected part) is this:
>>>>>>Err:tration?$filter=Modified%20gt%20datetime\'2014-04-30T00:00:00.000Z\'&$orderby=Mo<<<<<<<
Traceback (most recent call last):
File "TestURL.py", line 41, in <module>
j = json.loads(body)
File "c:\Python33\lib\json\__init__.py", line 316, in loads
return _default_decoder.decode(s)
File "c:\Python33\lib\json\decoder.py", line 351, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\Python33\lib\json\decoder.py", line 367, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 4005 column 114 (char 314020)
from
body = response.read().decode("utf-8")
print(">>>>>>Err:{}<<<<<<<".format(body[314020-40:314020+40]))
The string literal is not escaped correctly. Make sure the string really represent JSON.
>>> s = r'''{"etag": "W/\"1\""}''' # NOTE: raw string literal
>>> json.loads(s)
{'etag': 'W/"1"'}
The \' sequence is invalid JSON. Single quotes do not need to be escaped, making this an invalid string escape.
You could try to repair it after the fact:
import re
data = re.sub(r"(?<!\\)\\'", "'", data)
before loading it with JSON. This replaces \' with plain ', provided the backslash wasn't already escaped by a preceding \.
Since single quotes can only appear in string values, this should be safe.

Categories

Resources