user3769778
user3769778

Reputation: 967

Nodejs not parsing a stringified JSON, where as online validator are considering it as valid JSON?

I have this JSON string in my test.js file:

let a=JSON.parse(`{
    "context1": [{
        "ID": 4,
        "CONTEXT": "bye",
        "CREATED_TIME": "2017-12-17 03:56:53.761",
        "LAST_UPDATED_TIME": "2017-12-17 03:56:53.761"
    }],
    "context_INSERT": {
        "error": "StatementCallback; bad SQL grammar [insert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09')]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement \"insert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09[*]')\"; SQL statement:\ninsert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09') [42000-196]\n\tat org.h2.message.DbException.getJdbcSQLException(DbException.java:345)\n\tat org.h2.message.DbException.get(DbException.java:179)\n\tat org.h2.message.DbException.get(DbException.java:155)\n\tat org.h2.message.DbException.getSyntaxError(DbException.java:191)\n\tat org.h2.command.Parser.getSyntaxError(Parser.java:534)\n\tat org.h2.command.Parser.checkRunOver(Parser.java:3766)\n\tat org.h2.command.Parser.initialize(Parser.java:3677)\n\tat org.h2.command.Parser.parse(Parser.java:308)\n\tat org.h2.command.Parser.parse(Parser.java:297)\n\tat org.h2.command.Parser.prepareCommand(Parser.java:258)\n\tat org.h2.engine.Session.prepareLocal(Session.java:578)\n\tat org.h2.server.TcpServerThread.process(TcpServerThread.java:264)\n\tat org.h2.server.TcpServerThread.run(TcpServerThread.java:158)\n\tat java.lang.Thread.run(Unknown Source)\n"
    },
    "contexts": [{
        "ID": 3,
        "CONTEXT": "greetings",
        "CREATED_TIME": "2017-12-16 09:49:00.09",
        "LAST_UPDATED_TIME": "2017-12-16 09:49:00.09"
    }, {
        "ID": 4,
        "CONTEXT": "bye",
        "CREATED_TIME": "2017-12-17 03:56:53.761",
        "LAST_UPDATED_TIME": "2017-12-17 03:56:53.761"
    }]
}`);
console.log('done');
if(a.context_INSERT.error){
    console.log(a.context_INSERT.error);
}

Node gives me following error when I run above program:

undefined:9
                "error": "StatementCallback; bad SQL grammar [insert into context(context, created_time, last_updated_time) v
alues('someone',2017-12-16 09:49:00.09','2017-12-16 09:49:00.09')]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax
error in SQL statement "insert into context(context, created_time, last_updated_time) values('someone',2017-12-16 09:49:00.09
','2017-12-16 09:49:00.09[*]')"; SQL statement:


                        ^

SyntaxError: Unexpected token i in JSON at position 429
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (C:\Users\IBM_ADMIN\Desktop\temp\test1\test.js:1:74)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:598:3

However online JSON parser like : https://jsonlint.com/ is passing, the string as a valid JSON ?

Which one is correct and what change I need to do in my string to make it valid json for node?

Upvotes: 0

Views: 83

Answers (1)

Quentin
Quentin

Reputation: 943570

The string you are passing to the online validator is not the same as the string you are passing to JSON.parse!

JSON and JavaScript share a common set of escape characters.

The JavaScript parser will consume then when parsing your template string literal. This means that this section (for example):

SQL statement \"insert into context

includes an escaped double quote in JavaScript source code which represents a literal double quote in JSON.

Literal double quotes are not allowed in the middle of strings in JSON. The double quote ends the JSON string and the i that follows it is therefore invalid.

You need to escape the \ in the JavaScript source code so that it appears as an escape character in the JSON.

SQL statement \\"insert into context

There is almost never a good reason to embed JSON in a string literal in a JavaScript program. It is almost always better to just treat the JSON as literal JavaScript syntax.

let a = {
    "context1": [{
    // etc
};

Upvotes: 3

Related Questions