Cómo convertir postgres json a entero
to_json(1) to cast int to json, but how can I convert json to int? This may be too slow:
Also, is json wrapped from a binary block (bson) or a simple wrapper of text?
preguntado el 27 de noviembre de 13 a las 07:11
What works for me (using posgtgresql 5.6) is
SELECT (tablename.jsoncolumnname->>'jsonfiledname')::int FROM tablename;
SELECT (users.data->>'failed_login_attempts_count')::int FROM users;
users table has a json column named
data que es algo como:
SELECT (*)::int - That's what I was looking for! Thank you. - junaid atari
(ta.args ->> 'deposit')::bigint seemed like it was going to work, but I got
ERROR: value "1830000000000000000000" is out of range for type bigint - Ryan
Ryan, these answers should help you for sure. the problem is, you just cannot store that big number as integer, because then you lack efficiency in using integers as data types. stackoverflow.com/questions/7142604/… - Mochi
to_json(1)::text::int maybe too slow
But then, it's the only way.
The second part of your question is unclear.
The PostgreSQL 9.3 JSON support is simply validated json text.
In 9.4 and newer you can use jsonb.
"may be too slow" doesn't make a ton of sense. What makes you think it's too slow? Did you test and benchmark? If it's "too slow" what speed would no be too slow, i.e. what do you expect?
thank you, hope the new version published soon. since there is a transform between string and number, it will certainly slow than direct integer type, as everybody know. thow it can be accept in real-case useage, we are seeking more fast way. i put this question under json-to-int question, because i'd suspected json is wrapped from text, this let json-to-int must cast from text,otherwise,it's store structure should be already typed. - Inshua
jsonb now exists. Does it support directly extracting integers from json? - Tom Ellis
@Craig Ringer - it has problems if value is null: select ('[null]'::json->0)::text::int raises invalid input syntax for integer: "null" which makes sense ... this works: select ('[null]'::json->>0)::int ... returns null - Reinscerebro
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas json postgresql or haz tu propia pregunta.
Relacionado: How to convert Postgres json(b) to float?, ¿textear?, to boolean? - Bergi