array_to_json()

A function for converting an array to JSON

array_to_json() is a system function for converting an array to JSON representation.

array_to_json() was added in PostgreSQL 9.2.

Usage

array_to_json ( anyarray [, boolean ] ) → json

If the optional boolean parameter is set to TRUE, line breaks will be added between top-level array elements.

Change history

Examples

Basic usage examples for array_to_json():

postgres=# SELECT array_to_json(ARRAY[1,2,3]);
 array_to_json 
---------------
 [1,2,3]
(1 row)

postgres=# SELECT array_to_json(ARRAY[ [1,2,3],[4,5,6] ]);
   array_to_json   
-------------------
 [​[1,2,3],[4,5,6]​]
(1 row)

postgres=# SELECT array_to_json(ARRAY[ [1,2,3],[4,5,6] ], TRUE);
 array_to_json 
---------------
 [​[1,2,3],    +
  [4,5,6]​]
(1 row)

Categories

Arrays, JSON, System function

See also

array_to_string(), array_to_tsvector(), row_to_json(), hstore_to_json(), row_to_json()