informatica:base_de_datos:postgresql:postgresql_json
¡Esta es una revisión vieja del documento!
PostgreSQL JSON
select * from json_to_recordset( '{"a":{"b":[{"f1":2,"f2":4},{"f1":3,"f2":6}]}}'::json->'a'->'b' --inner table b ) as x("f1" int, "f2" int); --fields from table b
SELECT '{"sensor_id": "1234", "reading": {"temperature": 22.5, "unit": "C"}}'::jsonb -> 'reading' ->> 'unit' AS sensor_reading;
Probar si un JSON valido
<code postgresql> CREATE OR REPLACE FUNCTION f_is_json(_txt text)
RETURNS bool LANGUAGE plpgsql IMMUTABLE STRICT AS
$func$ BEGIN
RETURN _txt::json IS NOT NULL;
EXCEPTION
WHEN SQLSTATE '22P02' THEN -- invalid_text_representation
RETURN false;
END $func$;
COMMENT ON FUNCTION f_is_json(text) IS 'Test if input text is valid JSON. Returns true, false, or NULL on NULL input.' </cpde>
informatica/base_de_datos/postgresql/postgresql_json.1770855608.txt.gz · Última modificación: 2026/02/12 00:20 por admin
