Getting invalid JSON response, but JSON validates:

Getting invalid JSON response, but JSON validates:

ainglishainglish Posts: 9Questions: 2Answers: 0

Link to test case: private
Debugger code (debug.datatables.net): uferey
Error messages shown: Invalid JSON response
Description of problem: Getting invalid JSON response, but JSON validates

Answers

  • ainglishainglish Posts: 9Questions: 2Answers: 0

    I believe the issue has to be data-related, as the code works fine for other data sets generated in the same application. Can't figure out why DT is failing on this set, as the JSON validates in multiple validation tools.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Can you post an example of the JSON?

    Did you validate the JSON that is shown in the browser's network inspector tool?

    Without seeing the JSON string it will be hard to say what the problem might be. Or if you only want to share with the developers the debugger should have the JSON response.

    Kevin

  • ainglishainglish Posts: 9Questions: 2Answers: 0
    edited January 2022

    Yes, I validated the JSON from the network inspector tool.
    It validates on jsonlint.com, as well as jsonformatter.org and json.parser.online.fr.

    What's the best way to share the JSON here?

    The data set is fairly large (>300 records).

    JSON is currently shared via debugger.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    edited January 2022

    If you want to post a few lines of it here you can use Markdown formatting.

    If you think its data related you could create a simple test case, in live.datatables.net, jsfiddle, js bin, and try portions or all of the data by using the data option. Something like this Javascript example. This may help you narrow down the problem.

    Kevin

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    JSON is currently shared via debugger.

    You will need to provide the link or debugger id to the developers. Either here or directly PM them. Only the developers can open the debugger image.

    Kevin

  • ainglishainglish Posts: 9Questions: 2Answers: 0

    The debugger ID is listed in my opening post. I was hoping that the developers would review it. I sent Allan a PM, is there another user/group I should send to? I have support credits that can be used for this.

    @kthorngren - Thanks for helping me get up to speed. I'm new here. I will see if I can create a JS Fiddle as well.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Sorry, didn't see the id above :smile:

    Let us know what you find in the fiddle.

    Kevin

  • ainglishainglish Posts: 9Questions: 2Answers: 0

    So, I solved my issue. I'm not sure if this can qualify as a DataTables bug or not, but I want to document here in case it can help somebody else.

    The issue occurs when there is a 'tab' character in the JSON dataset used for the table source.

    Take for example a field containing a serial number. The value '123456' will cause no problems and the data table will render correctly.

    However, a prepended 'tab' character, '<tab>123456' will cause the data table to throw 'invalid JSON response', and the table will fail. This does pass JSON validation, so I'm not sure why the error is being thrown.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Sounds like tabs aren't allowed. See this SO thread. You can see the tab doesn't work in this example:
    http://live.datatables.net/vogarihi/1/edit

    Using JSON.parse('{"data": " 123456"}'); containing a tab fails but without the tab it works. Not sure why you don't see a failure with jsonlint.com. If I copy that string into jsonlint.com it fails.

    Kevin

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi,

    Apologies for having not been able to get back to you over the weekend.

    I can't help but feel there is something else going on here - I've just had a look at your debug trace (thank you for that), and it passes validation without any problems.

    Are you able to PM me a link to your page so I can take a deeper dive into what is going on?

    Thanks,
    Allan

  • ainglishainglish Posts: 9Questions: 2Answers: 0

    @kthorngren - You're absolutely right. Tabs are NOT allowed in JSON, and this was exactly my issue.

    Unfortunately, although the JSON response DID include a <tab> character in the data ("breaking" DataTables), the browser's network inspector tool was filtering it out.

    So DataTables was (correcty) complaining about bad JSON, but the network inspector was "fixing" the JSON (by removing the tab/whitespace) before displaying. Very frustrating to troubleshoot, since the result I was copying from the network inspector was valid!!!

    Anyway, I solved the issue by trimming whitespace before persisting or returning data.

    Issue closed. Hope this helps someone with the same issue in the future...

    (PS - Allan, thanks for the reply. DataTables is an amazing tool!)

Sign In or Register to comment.