Why doesn't JSON null apply to SQL null in postgres?

So, the following PostgreSQL snippet returns nullas it should:

select ('{"id": null}'::json->'id')

Intuitively, you can expect the following statement to return nulleither an empty string:

select ('{"id": null}'::json->'id')::TEXT

Instead, it returns the string "null". Why?

Besides,

select ('{"id": null}'::json->'id')::INTEGER

returns cannot cast type json to integerand

select ('{"id": null}'::json->'id')::TEXT::INTEGER

returns invalid input syntax for integer: "null". (In this case, JSON null listing for SQL null in the INTEGER column is used.)

There is a similar question with a somewhat unintelligible answer that seems to boil down to โ€œnull JSON values โ€‹โ€‹and null SQL values โ€‹โ€‹are slightly differentโ€ and no further explanation. Can someone help me understand what is going on here? This apparent behavior seems crazy!

? "" node / "" , yuck. ?

+4
2

->> json-.

null ( , ) :

select ('{"id": null}'::json->>'id')::text
select ('{"id": null}'::json->>'id')::integer

,

PS: "null", json : {"id": "null"}

+2

, json_typeof, , JSON, โ†’

json_typeof ('{ "id" : 4}':: json โ†’ 'id'), json_typeof ('{ "id" : "null" }':: json โ†’ 'id'), json_typeof ( '{ "id" : null}':: json โ†’ 'id')

โ†’ > , "null"

+1

Source: https://habr.com/ru/post/1652643/


All Articles