{"Entry":{"collection":"func","key":"to-json","name":"to_json","aliases":[],"metadata":{"changed_in":["9.4"],"changes":[{"descriptions_changed":false,"doc_overhaul":false,"from":"9.2","group_changed":null,"signatures":{"added":[],"removed":[]},"status":"added","to":"9.3"},{"descriptions_changed":true,"doc_overhaul":false,"from":"9.3","group_changed":null,"signatures":{"added":["to_json ( anyelement )"],"removed":["to_json ( anyelement ) → json"]},"status":"changed","to":"9.4"},{"descriptions_changed":true,"doc_overhaul":false,"from":"9.4","group_changed":null,"signatures":{"added":[],"removed":[]},"status":"changed","to":"9.5"},{"descriptions_changed":false,"doc_overhaul":true,"from":"12","group_changed":null,"signatures":{"added":[],"removed":[]},"status":"changed","to":"13"}],"content_hash":"8e445b766c7f48e5e27115762ca3ec8ddfc19aa7fcb662c057a3d1ae9e38160a","first_version":"9.3","group":"json","group_label":"Json Functions And Operators","groups":["json"],"imported_at":"2026-09-14T21:49:48.8189+08:00","last_version":"20","name":"to_json","name_key":"to_json","position":16059,"present_in":["9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"signature":"to_json ( anyelement ) → json","signature_count":1,"slug":"to-json","source_rev":"2026-09-14T13:14:25.684213+00:00@99e6866c","summary":"Converts any SQL value to json or jsonb.","summary_zh":""}},"Definition":{"Collection":"func","Key":"to-json","SourceDatabase":"center","Version":"18","SourceTable":"func","SourceKey":"to-json","SourceRevision":"2026-09-14T13:14:25.684213+00:00@99e6866c","Facts":{"description":"Converts any SQL value to json or jsonb. Arrays and composites are converted recursively to arrays and objects (multidimensional arrays become arrays of arrays in JSON). Otherwise, if there is a cast from the SQL data type to json, the cast function will be used to perform the conversion;[a] otherwise, a scalar JSON value is produced. For any scalar other than a number, a Boolean, or a null value, the text representation will be used, with escaping as necessary to make it a valid JSON string value.","description_html":"Converts any SQL value to \u003ccode class=\"type\"\u003ejson\u003c/code\u003e or \u003ccode class=\"type\"\u003ejsonb\u003c/code\u003e. Arrays and composites are converted recursively to arrays and objects (multidimensional arrays become arrays of arrays in JSON). Otherwise, if there is a cast from the SQL data type to \u003ccode class=\"type\"\u003ejson\u003c/code\u003e, the cast function will be used to perform the conversion;\u003ca href=\"/docs/18/functions-json.html#ftn.id-1.5.8.22.8.9.2.2.1.1.3.4\"\u003e\u003csup\u003e[a]\u003c/sup\u003e\u003c/a\u003e otherwise, a scalar JSON value is produced. For any scalar other than a number, a Boolean, or a null value, the text representation will be used, with escaping as necessary to make it a valid JSON string value.","description_zh":"","description_zh_html":"","doc":{"anchor":"FUNCTIONS-JSON-CREATION-TABLE","file":"functions-json.html","slug":"18"},"group":"json","group_label":"Json Functions And Operators","lang":"en","layout":"table-new","pages":["functions-json.html"],"prose_only":false,"signatures":[{"description":"Converts any SQL value to json or jsonb. Arrays and composites are converted recursively to arrays and objects (multidimensional arrays become arrays of arrays in JSON). Otherwise, if there is a cast from the SQL data type to json, the cast function will be used to perform the conversion;[a] otherwise, a scalar JSON value is produced. For any scalar other than a number, a Boolean, or a null value, the text representation will be used, with escaping as necessary to make it a valid JSON string value.","description_html":"Converts any SQL value to \u003ccode class=\"type\"\u003ejson\u003c/code\u003e or \u003ccode class=\"type\"\u003ejsonb\u003c/code\u003e. Arrays and composites are converted recursively to arrays and objects (multidimensional arrays become arrays of arrays in JSON). Otherwise, if there is a cast from the SQL data type to \u003ccode class=\"type\"\u003ejson\u003c/code\u003e, the cast function will be used to perform the conversion;\u003ca href=\"/docs/18/functions-json.html#ftn.id-1.5.8.22.8.9.2.2.1.1.3.4\"\u003e\u003csup\u003e[a]\u003c/sup\u003e\u003c/a\u003e otherwise, a scalar JSON value is produced. For any scalar other than a number, a Boolean, or a null value, the text representation will be used, with escaping as necessary to make it a valid JSON string value.","description_zh":"","description_zh_html":"","examples":[{"expr":"to_json('Fred said \"Hi.\"'::text)","result":"\"Fred said \\\"Hi.\\\"\""},{"expr":"to_jsonb(row(42, 'Fred said \"Hi.\"'::text))","result":"{\"f1\": 42, \"f2\": \"Fred said \\\"Hi.\\\"\"}"}],"html":"\u003ccode class=\"function\"\u003eto_json\u003c/code\u003e ( \u003ccode class=\"type\"\u003eanyelement\u003c/code\u003e ) → \u003ccode class=\"returnvalue\"\u003ejson\u003c/code\u003e","returns":"json","text":"to_json ( anyelement ) → json","zh_from":""}],"zh_from":""},"ManualEvidence":{"doc":{"anchor":"FUNCTIONS-JSON-CREATION-TABLE","file":"functions-json.html","slug":"18"}},"MeasuredEvidence":{}},"Text":{"Collection":"func","Key":"to-json","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"to_json","Summary":"将任何SQL值转换为json或jsonb。数组和复合值递归地转换为数组和对象（多维数组在JSON中变成数组的数组）。否则，如果存在从SQL数据类型到json的类型转换，则类型转换函数将用于执行转换；[a] 否则，将生成一个标量 JSON 值。对于除数字、布尔值或空值之外的任何标量，将使用文本表示，并根据需要进行转义，使其成为有效的JSON字符串值。","BodyHTML":"将任何SQL值转换为\u003ccode\u003ejson\u003c/code\u003e或\u003ccode\u003ejsonb\u003c/code\u003e。数组和复合值递归地转换为数组和对象（多维数组在JSON中变成数组的数组）。否则，如果存在从SQL数据类型到\u003ccode\u003ejson\u003c/code\u003e的类型转换，则类型转换函数将用于执行转换；\u003ca href=\"/docs/18/functions-json.html#ftn.id-1.5.8.22.8.9.2.2.1.1.3.4\" rel=\"nofollow\"\u003e\u003csup\u003e[a]\u003c/sup\u003e\u003c/a\u003e 否则，将生成一个标量 JSON 值。对于除数字、布尔值或空值之外的任何标量，将使用文本表示，并根据需要进行转义，使其成为有效的JSON字符串值。\u003cpre\u003eto_json ( anyelement ) → json\u003c/pre\u003e将任何SQL值转换为\u003ccode\u003ejson\u003c/code\u003e或\u003ccode\u003ejsonb\u003c/code\u003e。数组和复合值递归地转换为数组和对象（多维数组在JSON中变成数组的数组）。否则，如果存在从SQL数据类型到\u003ccode\u003ejson\u003c/code\u003e的类型转换，则类型转换函数将用于执行转换；\u003ca href=\"/docs/18/functions-json.html#ftn.id-1.5.8.22.8.9.2.2.1.1.3.4\" rel=\"nofollow\"\u003e\u003csup\u003e[a]\u003c/sup\u003e\u003c/a\u003e 否则，将生成一个标量 JSON 值。对于除数字、布尔值或空值之外的任何标量，将使用文本表示，并根据需要进行转义，使其成为有效的JSON字符串值。","SourceRevision":"ca936764","ContentHash":"8d0976d84db2b6da93a96c939ae0dbd4ffe26e7735e68e8054469ac094ffa13a","Payload":{"description_zh":"将任何SQL值转换为json或jsonb。数组和复合值递归地转换为数组和对象（多维数组在JSON中变成数组的数组）。否则，如果存在从SQL数据类型到json的类型转换，则类型转换函数将用于执行转换；[a] 否则，将生成一个标量 JSON 值。对于除数字、布尔值或空值之外的任何标量，将使用文本表示，并根据需要进行转义，使其成为有效的JSON字符串值。","description_zh_html":"将任何SQL值转换为\u003ccode class=\"type\"\u003ejson\u003c/code\u003e或\u003ccode class=\"type\"\u003ejsonb\u003c/code\u003e。数组和复合值递归地转换为数组和对象（多维数组在JSON中变成数组的数组）。否则，如果存在从SQL数据类型到\u003ccode class=\"type\"\u003ejson\u003c/code\u003e的类型转换，则类型转换函数将用于执行转换；\u003ca href=\"/docs/18/functions-json.html#ftn.id-1.5.8.22.8.9.2.2.1.1.3.4\"\u003e\u003csup\u003e[a]\u003c/sup\u003e\u003c/a\u003e 否则，将生成一个标量 JSON 值。对于除数字、布尔值或空值之外的任何标量，将使用文本表示，并根据需要进行转义，使其成为有效的JSON字符串值。","signatures":[{"description_zh":"将任何SQL值转换为json或jsonb。数组和复合值递归地转换为数组和对象（多维数组在JSON中变成数组的数组）。否则，如果存在从SQL数据类型到json的类型转换，则类型转换函数将用于执行转换；[a] 否则，将生成一个标量 JSON 值。对于除数字、布尔值或空值之外的任何标量，将使用文本表示，并根据需要进行转义，使其成为有效的JSON字符串值。","description_zh_html":"将任何SQL值转换为\u003ccode class=\"type\"\u003ejson\u003c/code\u003e或\u003ccode class=\"type\"\u003ejsonb\u003c/code\u003e。数组和复合值递归地转换为数组和对象（多维数组在JSON中变成数组的数组）。否则，如果存在从SQL数据类型到\u003ccode class=\"type\"\u003ejson\u003c/code\u003e的类型转换，则类型转换函数将用于执行转换；\u003ca href=\"/docs/18/functions-json.html#ftn.id-1.5.8.22.8.9.2.2.1.1.3.4\"\u003e\u003csup\u003e[a]\u003c/sup\u003e\u003c/a\u003e 否则，将生成一个标量 JSON 值。对于除数字、布尔值或空值之外的任何标量，将使用文本表示，并根据需要进行转义，使其成为有效的JSON字符串值。","returns":"json","text":"to_json ( anyelement ) → json","zh_from":"doc"}],"zh_from":"doc"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":[{"Version":"18","Ordinal":0,"Hash":"e769bc80c16a50e70cb57a5f7418fd562058283bbab8b16372d9582abe753a15","Text":"to_json ( anyelement ) → json","Returns":"json","Payload":{"description":"Converts any SQL value to json or jsonb. Arrays and composites are converted recursively to arrays and objects (multidimensional arrays become arrays of arrays in JSON). Otherwise, if there is a cast from the SQL data type to json, the cast function will be used to perform the conversion;[a] otherwise, a scalar JSON value is produced. For any scalar other than a number, a Boolean, or a null value, the text representation will be used, with escaping as necessary to make it a valid JSON string value.","description_html":"Converts any SQL value to \u003ccode class=\"type\"\u003ejson\u003c/code\u003e or \u003ccode class=\"type\"\u003ejsonb\u003c/code\u003e. Arrays and composites are converted recursively to arrays and objects (multidimensional arrays become arrays of arrays in JSON). Otherwise, if there is a cast from the SQL data type to \u003ccode class=\"type\"\u003ejson\u003c/code\u003e, the cast function will be used to perform the conversion;\u003ca href=\"/docs/18/functions-json.html#ftn.id-1.5.8.22.8.9.2.2.1.1.3.4\"\u003e\u003csup\u003e[a]\u003c/sup\u003e\u003c/a\u003e otherwise, a scalar JSON value is produced. For any scalar other than a number, a Boolean, or a null value, the text representation will be used, with escaping as necessary to make it a valid JSON string value.","description_zh":"","description_zh_html":"","examples":[{"expr":"to_json('Fred said \"Hi.\"'::text)","result":"\"Fred said \\\"Hi.\\\"\""},{"expr":"to_jsonb(row(42, 'Fred said \"Hi.\"'::text))","result":"{\"f1\": 42, \"f2\": \"Fred said \\\"Hi.\\\"\"}"}],"html":"\u003ccode class=\"function\"\u003eto_json\u003c/code\u003e ( \u003ccode class=\"type\"\u003eanyelement\u003c/code\u003e ) → \u003ccode class=\"returnvalue\"\u003ejson\u003c/code\u003e","returns":"json","text":"to_json ( anyelement ) → json","zh_from":""}}],"Spellings":null,"SQLState":null,"Evidence":null}
