Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Applies to:
Databricks SQL
Databricks Runtime
Returns a JSON string with the STRUCT or VARIANT specified in expr.
Syntax
to_json(expr [, options] )
Arguments
expr: ASTRUCTexpression, or aVARIANTin Databricks SQL and Databricks Runtime 15.3 and above.options: An optionalMAPliteral expression with keys and values beingSTRING. Ifexpris aVARIANT, the options are ignored.
Returns
A STRING.
For a complete list of options, see JSON.
Examples
> SELECT to_json(named_struct('a', 1, 'b', 2));
{"a":1,"b":2}
> SELECT to_json(named_struct('time', to_timestamp('2015-08-26', 'yyyy-MM-dd')), map('timestampFormat', 'dd/MM/yyyy'));
{"time":"26/08/2015"}
> SELECT to_json(array(named_struct('a', 1, 'b', 2)));
[{"a":1,"b":2}]
> SELECT to_json(map('a', named_struct('b', 1)));
{"a":{"b":1}}
> SELECT to_json(map(named_struct('a', 1),named_struct('b', 2)));
{"[1]":{"b":2}}
> SELECT to_json(map('a', 1));
{"a":1}
> SELECT to_json(array((map('a', 1))));
[{"a":1}]
-- VARIANT input
> SELECT to_json(parse_json('{"key": 123, "data": [4, 5, "str"]}'))
{"data":[4,5,"str"],"key":123}