Python Forum
Error when decoding JSON ( Expecting ',' delimiter:)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when decoding JSON ( Expecting ',' delimiter:)
#1
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"
}
Reply
#2
This is valid JSON. Are you sure you identified the problem part correctly?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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"
Reply
#7
That still looks perfectly valid.
Can you show us the code that makes the request?
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  expecting value: line 1 column 1 (char 0) in print (r.json)) loutsi 3 1,046 Jun-05-2020, 08:38 PM
Last Post: nuffink
  500 error cannot deserialize json object SaiBhargav 0 1,133 Sep-18-2018, 09:53 PM
Last Post: SaiBhargav
  Decoding html to text string PeterPython 1 1,127 Aug-12-2018, 07:23 PM
Last Post: Larz60+
  Json decoding error saisankalpj 3 1,737 Jul-23-2018, 07:06 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020