Comments (6)
They're not supposed to be equivalent. to_json
turns a string into a JSON-string (a JSON object that contains a single string; in your case, a string that happens to contains braces and quotes), whereas ::JSON
parses a string representation of a JSON object into a JSON object.
Check
https://duckdb.org/docs/extensions/json.html
-- since to_json('duck')
has to return a JSON-string (a JSON object that contains the single string "duck"
, your result for to_json(<yourstring>)
is the correct result too.
from duckdb.
@szarnyasg I think you should close this.
from duckdb.
It's OK that TO_JSON does not work the same as in PostgreSQL.
I just didn't understand how it works from the DOC:
https://duckdb.org/docs/extensions/json.html#json-creation-functions
There is no single line about JSON-string and escaped enclosers.
Would it be worth extending the DOC?
from duckdb.
Btw it's weird to me that both TO_JSON(column)
and column::JSON
return the same data type(JSON), but the content behaves differently.
from duckdb.
I agree: if this difference is actually desired, it's worth adding some lines to the documentation.
from duckdb.
Thanks for creating this issue! @soerenwolfers is right. Casting VARCHAR
to JSON
is a special cast, as it will parse. Using to_json
will convert the input to JSON by interpreting it as a string. This is the same behavior as Postgres AFAIK.
postgres=# select to_json('{"duck": 42}');
ERROR: could not determine polymorphic type because input has type unknown
postgres=# select to_json('{"duck": 42}'::VARCHAR);
to_json
------------------
"{\"duck\": 42}"
(1 row)
postgres=# select to_json('{"duck": 42}'::JSON);
to_json
--------------
{"duck": 42}
(1 row)
postgres=# select '{"duck": 42}'::JSON;
json
--------------
{"duck": 42}
(1 row)
D select to_json('{"duck": 42}');
┌─────────────────────────┐
│ to_json('{"duck": 42}') │
│ json │
├─────────────────────────┤
│ "{\"duck\": 42}" │
└─────────────────────────┘
D select to_json('{"duck": 42}'::VARCHAR);
┌──────────────────────────────────────────┐
│ to_json(CAST('{"duck": 42}' AS VARCHAR)) │
│ json │
├──────────────────────────────────────────┤
│ "{\"duck\": 42}" │
└──────────────────────────────────────────┘
D select to_json('{"duck": 42}'::JSON);
┌───────────────────────────────────────┐
│ to_json(CAST('{"duck": 42}' AS JSON)) │
│ json │
├───────────────────────────────────────┤
│ {"duck":42} │
└───────────────────────────────────────┘
D select '{"duck": 42}'::JSON;
┌──────────────────────────────┐
│ CAST('{"duck": 42}' AS JSON) │
│ json │
├──────────────────────────────┤
│ {"duck": 42} │
└──────────────────────────────┘
from duckdb.
Related Issues (20)
- Performance difference with `read_csv` between a file and stdin HOT 1
- Query on Arrow views fail with specific order of filters HOT 2
- Review patches from duckdb-r HOT 1
- READ_CSV doesn't release memory in between files it imports HOT 3
- Double inserts HOT 1
- make parameter handling orthogonal, please HOT 2
- TPC-H extension generates identical values on WASM client
- read parquet file from s3 1.0.0 slower than 0.9.2 HOT 5
- terminate called after throwing an instance of 'duckdb::InvalidInputException' when read parquet from minio HOT 4
- Incorrect file type when writing files to google cloud bucket via httpfs
- BETWEEN incorrectly pushed into ASOF JOIN HOT 4
- cmake target exports use fixed paths (not relocatable)
- .lint fkey-indexes terminates cli HOT 1
- Regression in 1.0 for representation of SQLNULL in Python API HOT 1
- INTERNAL Error reading from CSV file HOT 3
- C++ API crashes when running in debug mode with release mode dll.
- Invalid Input Error: CSV Error on Line: 729
- Silent exit after 31 x 2 GB GZIP-ed CSV imports
- No granular progress bar when importing a single 2 GB, GZIP-ed CSV
- Importing 2 GB GZIP'ed CSVs into DuckDB table take longer after each insert
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from duckdb.