skip to Main Content

Good morning everyone and thank you in advance for any suggestions. I have written a small web application to perform simple searches in a stamps database using php and javascript.

The server sends to the browser the whole database as a JSON and the queries are done client-side with a javascript code.
The JSON has this structure:
{"ck":0,"db":[["string11","string12","string13"],["string21","string22","string23"], etc... } .

Until now the system has worked perfectly and over 1500 stamps could be shown.
Suddenly it stopped working and, in the browser’s Javascript console, this error message appeared:
VM672:1 Uncaught SyntaxError: Expected ',' or ']' after array element in JSON at position 97506 at JSON.parse (<anonymous>) ...etc...

After a series of tests, by exclusion I came to discover that it was the word "annullo" in the last added record to generate the error.

I guess it could be the substring "null" to give problems, but I have no idea how to escape it.
A really strange thing is that, whilst failing with the JSON.parse() function, browser’s javascript console, as well as other json validation tools, recognise the server’s response as a valid JSON.

Thanks for any help!

2

Answers


  1. I think I understand the problem. All strings are enclosed in quotes.

    {example: "example"}
    

    In the development of this case they must have put "annullo" but they forgot to insert the quotes. If it was null, not a string, it could look like this, without quotes.

    json = {example: null}

    However, as annullo is an invalid word in javascript and is not as a string, it gives an error.

    The correct thing to do is to correct the error in the source, transforming it to null, however, I believe that you can also use this regular expression. But do tests to ensure that there is no undue substitution elsewhere.

    json.replace(/.+["']: ?annull(o|are)[n,}]$/g, matched => {
        return matched.replace(/annull(o|are)/, 'null')
    })
    
    Login or Signup to reply.
  2. As @Andrea Soffiantini pointed in a comment, this was the actual problem:

    Client side I had this command a few lines before parsing: data = data.replaceAll("null",""""); , where data is the json as a string, as received from server.

    This replaces the string "anullo" with "a""o", which is invalid in JSON syntax.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search