Giter Club home page Giter Club logo

Comments (6)

soerenwolfers avatar soerenwolfers commented on June 10, 2024

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.

soerenwolfers avatar soerenwolfers commented on June 10, 2024

@szarnyasg I think you should close this.

from duckdb.

jaceksan avatar jaceksan commented on June 10, 2024

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.

jaceksan avatar jaceksan commented on June 10, 2024

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.

soerenwolfers avatar soerenwolfers commented on June 10, 2024

I agree: if this difference is actually desired, it's worth adding some lines to the documentation.

from duckdb.

lnkuiper avatar lnkuiper commented on June 10, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.