The JSON format allows you to read and write JSON data based on a JSON schema. Currently, the JSON schema is derived from table schema. For details, see JSON Format.
Parameter |
Mandatory |
Default Value |
Type |
Description |
|---|---|---|---|---|
format |
Yes |
None |
String |
Format to be used. Set this parameter to json. |
json.fail-on-missing-field |
No |
false |
Boolean |
Whether missing fields and rows will be skipped or failed. The default value is false, indicating that an error will be thrown. |
json.ignore-parse-errors |
No |
false |
Boolean |
Whether fields and rows with parse errors will be skipped or failed. The default value is false, indicating that an error will be thrown. Fields are set to null in case of errors. |
json.timestamp-format.standard |
No |
'SQL' |
String |
Specify the input and output timestamp format for TIMESTAMP and TIMESTAMP_LTZ type. Currently supported values are SQL and ISO-8601:
|
json.map-null-key.mode |
No |
'FALL' |
String |
Handling mode when serializing null keys for map data. Available values are as follows:
|
json.map-null-key.literal |
No |
'null' |
String |
String literal to replace null key when json.map-null-key.mode is LITERAL. |
json.encode.decimal-as-plain-number |
No |
false |
Boolean |
Encode all decimals as plain numbers instead of possible scientific notations. For example, 0.000000027 is encoded as 2.7E-8 by default, and will be written as 0.000000027 if set this parameter to true. |
Currently, the JSON schema is always derived from table schema. Explicitly defining a JSON schema is not supported yet.
Flink JSON format uses jackson databind API to parse and generate JSON string.
The following table lists the type mapping from Flink type to JSON type.
Flink SQL Type |
JSON Type |
|---|---|
CHAR/VARCHAR/STRING |
String |
BOOLEAN |
Boolean |
BINARY/VARBINARY |
string with encoding: base64 |
DECIMAL |
Number |
TINYINT |
Number |
SMALLINT |
Number |
INT |
Number |
BIGINT |
Number |
FLOAT |
Number |
DOUBLE |
Number |
DATE |
string with format: date |
TIME |
string with format: time |
TIMESTAMP |
string with format: date-time |
TIMESTAMP_WITH_LOCAL_TIME_ZONE |
string with format: date-time (with UTC time zone) |
INTERVAL |
Number |
ARRAY |
array |
MAP / MULTISET |
object |
ROW |
object |
In this example, data is read from a topic and written to another using a Kafka sink.
CREATE TABLE kafkaSource ( order_id string, order_channel string, order_time string, pay_amount double, real_pay double, pay_time string, user_id string, user_name string, area_id string ) WITH ( 'connector' = 'kafka', 'topic' = 'kafkaTopic', 'properties.bootstrap.servers' = 'KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort', 'properties.group.id' = 'GroupId', 'scan.startup.mode' = 'latest-offset', 'format' = 'json' ); CREATE TABLE printSink ( order_id string, order_channel string, order_time string, pay_amount double, real_pay double, pay_time string, user_id string, user_name string, area_id string ) WITH ( 'connector' = 'print' ); insert into printSink select * from kafkaSource;
{"order_id":"202103241000000001","order_channel":"webShop","order_time":"2021-03-24 10:00:00","pay_amount":100.0,"real_pay":100.0,"pay_time":"2021-03-24 10:02:03","user_id":"0001","user_name":"Alice","area_id":"330106"}
{"order_id":"202103241606060001","order_channel":"appShop","order_time":"2021-03-24 16:06:06","pay_amount":200.0,"real_pay":180.0,"pay_time":"2021-03-24 16:10:06","user_id":"0001","user_name":"Alice","area_id":"330106"}
+I[202103241000000001, webShop, 2021-03-24 10:00:00, 100.0, 100.0, 2021-03-24 10:02:03, 0001, Alice, 330106] +I[202103241606060001, appShop11, 2021-03-24 16:06:06, 200.0, 180.0, 2021-03-24 16:10:06, 0001, Alice, 330106]