Herramientas de usuario

Herramientas del sitio


informatica:base_de_datos:postgresql:postgresql_json

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anteriorRevisión previa
Próxima revisión
Revisión previa
informatica:base_de_datos:postgresql:postgresql_json [2026/01/26 00:16] admininformatica:base_de_datos:postgresql:postgresql_json [2026/02/12 01:10] (actual) – [Probar si un JSON valido] admin
Línea 11: Línea 11:
  
   * [[https://www.postgresql.org/docs/9.5/functions-json.html]]   * [[https://www.postgresql.org/docs/9.5/functions-json.html]]
 +  * [[https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_each]]
 +
 +  * [[https://www.postgresql.org/docs/current/functions-json.html]]
  
 <code postgresql> <code postgresql>
Línea 25: Línea 28:
 SELECT '{"sensor_id": "1234", "reading": {"temperature": 22.5, "unit": "C"}}'::jsonb -> 'reading' ->> 'unit' AS sensor_reading; SELECT '{"sensor_id": "1234", "reading": {"temperature": 22.5, "unit": "C"}}'::jsonb -> 'reading' ->> 'unit' AS sensor_reading;
 </code> </code>
 +
 +===== Probar si un JSON es valido =====
 +
 +  * [[https://stackoverflow.com/questions/30187554/how-to-verify-a-string-is-valid-json-in-postgresql]]
 +    * [[https://stackoverflow.com/questions/30187554/how-to-verify-a-string-is-valid-json-in-postgresql/30187851#30187851]]
 +
 +<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.'
 +</code>
 +
 +
informatica/base_de_datos/postgresql/postgresql_json.1769386560.txt.gz · Última modificación: 2026/01/26 00:16 por admin