My favorites | Sign in
Project Home Wiki Issues Source
Project Information
Members

The project is designed to perform simple task: serialize PostgreSQL variables data (native and user-defined PostgreSQL types) to JSON format. Project's API is set of functions (implemented in C language), that can be used for serialization data of different types. JSON data can be parsed with many libraries in any actual programming language, so different complex data types (including arrays, composite and nested types) with this serializer can be used as native data.

Example of call:

 select to_json( row( 10, 'Some text', 12.5, row( 'text in nested record', array[ 1, 2, 3 ] )::text_and_array, array[ 'array', 'of', 'text' ] ) ) 

Output:

{"f1":10,"f2":"Some text","f3":12.5,"f4":{"str":"text in nested record","arr":[1,2,3]},"f5":["array","of","text"]}

Any query result dataset can be serialized with follow syntax:

 select to_json( q.* ) from ... as q
 

Multirow datasets also can be serialized with aggregate function json_agg.

Examples of using:

 select json_agg( q.*, 'json_field_name' ), json_agg_plain( q.*, 'json_field_name' ) from ... as q;
 

Output:

{"json_field_name":[{ ... },{ ... }, ...]}"json_field_name":[{ ... },{ ... }, ...]

Now pg-to-json-serializer also support functionality for parsing JSON strings and filling postgreSQL records/arrays from it.

Example:

 select from_json( 'some_record_type', '{"field1":"some text","field2":123,"field3":["this","is","array","of","text"]}' );
 select arr_from_json( 'some_type[]', '[{"this is array of"},{"records with one field"}]' )::text[];
 

Output:

("some text",123,"{\"this\",\"is\",\"array\",\"of\",\"text\"}")
{("this is array of"),("records with one field")}

Powered by Google Project Hosting