Python Forum

Full Version: Error when decoding JSON ( Expecting ',' delimiter:)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I use Python 3.6.3 on the wildfly server.

I have two different REST services. When I call one of them (getMC) on the SoapUI, there occurs an error. I found a piece of JSON which causes it, but I don't know and don't understand why.

Bad piece of JSON:
Quote:"cookieId": "1548768911871854",
// sth more


Error occurs in this line of my Python script:
jattrs = request.get_json()
Error message:
Error:
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 1088057 (char 1088056)
What's interesting, when I change the cookieId value (digits, letters, same length), it works. The second service with the above JSON works properly too.

Do you have any idea how to solve my problem? If you need more details, feel free to ask.

Full not-working JSON:

Quote:{
"cookieId": "1548768911871854",
"firstVisit": 0,
"customerExtId": null,
"channelName": "www",
"referrer": null,
"actionMcType": null,
"offerType": null,
"actionOnProduct": null,
"pageTitle": "page title",
"pageDesc": "page description",
"pageKeywords": "page kw"
}
This is valid JSON. Are you sure you identified the problem part correctly?
Yes, I'm pretty sure. JSON is correct, but the Python can't parse it. I investigated it step by step and that's the only case when it doesn't work.

When I changed cookieId="1549013632697518" in the above JSON, it works properly.
Python is definitely able to parse it:
>>> json.loads('''{
... "cookieId": "1548768911871854",
... "firstVisit": 0,
... "customerExtId": null,
... "channelName": "www",
... "referrer": null,
... "actionMcType": null,
... "offerType": null,
... "actionOnProduct": null,
... "pageTitle": "page title",
... "pageDesc": "page description",
... "pageKeywords": "page kw"
... }''')
{'cookieId': '1548768911871854', 'firstVisit': 0, 'customerExtId': None, 'channelName': 'www', 'referrer': None, 'action
McType': None, 'offerType': None, 'actionOnProduct': None, 'pageTitle': 'page title', 'pageDesc': 'page description', 'p
ageKeywords': 'page kw'}
It looks like you're misdiagnosing the problem.
If you can share code which recreates the error, we might be able to help.
Service calls this function.
@app.route('/json-data', methods=['POST'])
def getMC():

    logMC = logging.getLogger(__name__)
    defineLogging()
    logMC.info('Starting main function')

    try:

        logMC.info('Control point 1')

        jattrs = request.get_json()   

        logMC.info('Control point 2')
The "Control point 1" is logged, "Control point 2" is not, so I guess the line between them must be broken.

It doesn't work only for this one cookieId value... I thought that maybe Python tries to cast string to int and it's too long, but it's not a problem.
My mistake. I forgot that the API which calls Python modifies the entry JSON. It still does not work only for the one cookieId, but the broken char in JSON is ')' (bold below).

Quote:"browserUserAgent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
That still looks perfectly valid.
Can you show us the code that makes the request?
Uff I found the mistake. One value in the final JSON contained " (quotation mark) and Python thought it was the end of the value (not part of it). It works for other cookieId, because this one is the only one with this typo.

Thanks for your time and your help.