Python Forum
Error when decoding JSON ( Expecting ',' delimiter:) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Error when decoding JSON ( Expecting ',' delimiter:) (/thread-16139.html)



Error when decoding JSON ( Expecting ',' delimiter:) - tortoise - Feb-15-2019

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"
}



RE: Error when decoding JSON ( Expecting ',' delimiter:) - buran - Feb-15-2019

This is valid JSON. Are you sure you identified the problem part correctly?


RE: Error when decoding JSON ( Expecting ',' delimiter:) - tortoise - Feb-15-2019

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.


RE: Error when decoding JSON ( Expecting ',' delimiter:) - stranac - Feb-15-2019

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.


RE: Error when decoding JSON ( Expecting ',' delimiter:) - tortoise - Feb-16-2019

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.


RE: Error when decoding JSON ( Expecting ',' delimiter:) - tortoise - Feb-18-2019

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"



RE: Error when decoding JSON ( Expecting ',' delimiter:) - stranac - Feb-18-2019

That still looks perfectly valid.
Can you show us the code that makes the request?


RE: Error when decoding JSON ( Expecting ',' delimiter:) - tortoise - Feb-18-2019

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.