Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot parse/access json response of query: Bad escaped character #180

Closed
fundef1 opened this issue Sep 21, 2023 · 3 comments
Closed

cannot parse/access json response of query: Bad escaped character #180

fundef1 opened this issue Sep 21, 2023 · 3 comments

Comments

@fundef1
Copy link

fundef1 commented Sep 21, 2023

Hi, I'm trying to access the result of a query via json but it seems the output isn't propery escaped.
my table contains a regex but that's irrelevant, I think, it's just a VARCHAR.

my failed attempt:

const q = await vg.coordinator().query('FROM allregexes;', {type: "json", cache: false});
JSON.parse(q); // fails: Bad escaped character in JSON at position 32
console.log(q.toString()); // because the \d should be escaped as \\d {"regex": "^(?P<DD>0[1-9]|[1-2]\d|3[0-1])...

is this the correct way to access the query results via JSON, or did i find a bug?

curently, my workaround looks like this but would prefer json

const q = await vg.coordinator().query('FROM allregexes;', {type: "arrow", cache: false});
for (const entry of q) {
	console.log(entry.regex)
}

is this the right approach?

thanks!

@jheer
Copy link
Member

jheer commented Sep 21, 2023

Thanks for reporting the issue. Can you share a basic table CREATE and/or INSERT query with data that replicates the issue? (A single row table should be fine!) Then we can test directly on our end and see where in the pipeline the bug may arise.

@fundef1
Copy link
Author

fundef1 commented Sep 21, 2023

here you go:

vg.coordinator().exec('CREATE TABLE test AS SELECT \'^(?P<DD>0[1-9]|[1-2]\\d|3[0-1])/\' as teststring' );
vg.coordinator().query("SELECT teststring from test;", {type: "json", cache: false})
    .then((q) => {
            console.log(q.toString());
	    const t = JSON.parse(q);
            console.log(t);
	});

results in:

{"teststring": "^(?P<DD>0[1-9]|[1-2]\d|3[0-1])/"}
VM262:1 Uncaught (in promise) SyntaxError: Bad escaped character in JSON at position 37
    at JSON.parse (<anonymous>)
    at index.html:57:19
```

@jheer
Copy link
Member

jheer commented Sep 21, 2023

Thanks! I tested this and the problem is that the return value of the query is already parsed. Instead of calling JSON.parse, simply use the returned q array.

If using a DuckDB server (socket connector), the result will be sent back as a JSON string and parsed internally. Meanwhile, DuckDB-WASM (wasm connector) only produces Arrow tables, so when json is requested the Arrow table is coerced to a JS-Array-of-Objects compatible array.

@fundef1 fundef1 closed this as completed Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants