> This page location: JSON Functions > row_to_json > Full Neon documentation index: https://neon.com/docs/llms.txt # PostgreSQL row_to_json() Function **Summary**: in this tutorial, you will learn how to use the PostgreSQL `row_to_json()` function to convert an SQL composite value to a JSON object. ## Introduction to the PostgreSQL row_to_json() function The `row_to_json()` function allows you to convert an SQL composite value into a JSON object. Here's the syntax of the `row_to_json()` function: ```sql row_to_json ( record [, boolean ] ) → json ``` In this syntax: - `record` is an SQL composite value that you want to convert into a JSON object. - `boolean` if true, the function will add a line feed between top-level elements. The `row_to_json()` function will return a JSON object. ## PostgreSQL row_to_json() function examples Let's take some examples of using the `row_to_json()` function. ### 1) Basic row_to_json() function example The following example uses the `row_to_json()` function to convert a row into a JSON object: ```sql SELECT row_to_json(row('John',20)); ``` Output: ```text row_to_json ----------------------- {"f1":"John","f2":20} (1 row) ``` In this example, we use the `row()` function to create a composite value made up of multiple columns. The `row_to_json()` function returns an object whose keys are automatically generated f1 and f2 with the values from the composite values. ### 2) Using the row_to_json() function with table data We'll use the `film` table from the [sample database](../postgresql-getting-started/postgresql-sample-database): ![](https://neon.com/postgresqltutorial/film.png)The following example uses the `row_to_json()` function to convert the `title` and `length` of each film in the `film` table into a JSON object: ```sql SELECT row_to_json(t) film FROM ( SELECT title, length FROM film ORDER BY title ) t; ``` Output: ```text film ------------------------------------------------------ {"title":"Academy Dinosaur","length":86} {"title":"Ace Goldfinger","length":48} {"title":"Adaptation Holes","length":50} {"title":"Affair Prejudice","length":117} {"title":"African Egg","length":130} {"title":"Agent Truman","length":169} ... ``` How it works. - The subquery retrieves the `title` and `length` from the `film` table. - The outer query uses the `row_to_json()` to convert each row returned by the subquery into a JSON object. Note that you can use a [common table expression](../postgresql-tutorial/postgresql-cte) (`CTE`) instead of a subquery to achieve the same result: ```sql WITH film_cte AS ( SELECT title, length FROM film ORDER BY title ) SELECT row_to_json(film_cte) FROM film_cte; ``` ## Summary - Use the `row_to_json()` function to convert an SQL composite value to a JSON object. --- ## Related docs (JSON Functions) - [Extracting JSON Data](https://neon.com/postgresql/postgresql-json-functions/postgresql-json-extract) - [JSONB Operators](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb-operators) - [jsonb_agg](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_agg) - [jsonb_array_elements](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_array_elements) - [jsonb_array_elements_text](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_array_elements_text) - [jsonb_array_length](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_array_length) - [jsonb_build_array](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_build_array) - [jsonb_build_object](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_build_object) - [jsonb_each](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_each) - [jsonb_each_text](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_each_text) - [jsonb_extract_path](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_extract_path) - [jsonb_extract_path_text](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_extract_path_text) - [jsonb_insert](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_insert) - [jsonb_object](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_object) - [jsonb_object_agg](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_object_agg) - [jsonb_object_keys](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_object_keys) - [JSON Path](https://neon.com/postgresql/postgresql-json-functions/postgresql-json-path) - [jsonb_path_exists](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_exists) - [jsonb_path_query](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_query) - [jsonb_path_query_array](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_query_array) - [jsonb_path_query_first](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_path_query_first) - [jsonb_pretty](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_pretty) - [jsonb_set](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_set) - [jsonb_strip_nulls](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_strip_nulls) - [jsonb_to_record](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_to_record) - [jsonb_populate_record](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_populate_record) - [jsonb_populate_recordset](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_populate_recordset) - [jsonb_typeof](https://neon.com/postgresql/postgresql-json-functions/postgresql-jsonb_typeof) - [to_jsonb](https://neon.com/postgresql/postgresql-json-functions/postgresql-to_jsonb)