Database Reference
In-Depth Information
Returning data in the JSON format
In PostgreSQL 9.2, there is a new native data type for JSON values. This new sup-
port for JSON is following the same growth pattern as XML. It initially started with
two functions for converting arrays and records to JSON format, but in PostgreSQL
9.3 there will be more functions.
The current functions are row_to_json(record, bool) for converting any re-
cord to JSON, and array_to_json(anyarray, bool) for converting any array
to JSON.
The following are some simple examples of using these functions:
hannu=# select array_to_json(array[1,2,3]);
-[ RECORD 1 ]-+--------
array_to_json | [1,2,3]
hannu=# select * from test;
-[ RECORD 1 ]--------------------
id | 1
data | 0.26281
tstampt | 2012-04-05 13:21:03.235
-[ RECORD 2 ]--------------------
id | 2
data | 0.1574
tstampt | 2012-04-05 13:21:05.201
hannu=# select row_to_json(t) from test t;
-[ RECORD 1
]------------------------------------------------------------
row_to_json |
{"id":1,"data":0.26281,"tstampt":"2012-04-05
13:21:03.235"}
-[ RECORD 2
]------------------------------------------------------------
row_to_json |
Search WWH ::




Custom Search