跳转到主要内容

jsonb_plpython3u

transform between jsonb and plpython3u

plpython3u : transform between jsonb and plpython3u

Overview

ID Extension Package Version Category License Language
3291 jsonb_plpython3u plpython3u 1.0 LANG PostgreSQL C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
----d-r No No No Yes yes no

Packages

PG18 PG17 PG16 PG15 PG14
1.0 1.0 1.0 1.0 1.0
This is a built-in contrib extension ship with the PostgreSQL kernel

Install

Create this extension with:

CREATE EXTENSION jsonb_plpython3u CASCADE; -- requires plpython3u

Usage

jsonb_plpython3u: Transform between jsonb and PL/Python3

Provides a transform for the jsonb type for PL/Python3U. When loaded, jsonb values are automatically converted to Python dicts, lists, and scalars, and vice versa.

CREATE EXTENSION jsonb_plpython3u;

CREATE FUNCTION jsonb_keys_sorted(val jsonb) RETURNS text[]
LANGUAGE plpython3u TRANSFORM FOR TYPE jsonb AS $$
  # val is now a Python dict
  return sorted(val.keys())
$$;

CREATE FUNCTION build_jsonb(name text, age integer) RETURNS jsonb
LANGUAGE plpython3u TRANSFORM FOR TYPE jsonb AS $$
  return {"name": name, "age": age}
$$;

SELECT jsonb_keys_sorted('{"b": 2, "a": 1, "c": 3}'::jsonb);
SELECT build_jsonb('Alice', 30);