Description
In this format, all data is represented as a single JSON Object, with each row represented as a separate field of this object similar to theJSONEachRow format.
Example usage
Basic example
Given some JSON:format_json_object_each_row_column_for_object_name.
The value of this setting is set to the name of a column, that is used as JSON key for a row in the resulting object.
Output
Let’s say we have the tabletest with two columns:
JSONObjectEachRow format and use the format_json_object_each_row_column_for_object_name setting:
Query
Response
Input
Let’s say we stored the output from the previous example in a file nameddata.json:
Query
Response
Query
Response
Inserting data
Query
- Any order of key-value pairs in the object.
- Omitting some values.
Omitted values processing
ClickHouse substitutes omitted values with the default values for the corresponding data types. IfDEFAULT expr is specified, ClickHouse uses different substitution rules depending on the input_format_defaults_for_omitted_fields setting.
Consider the following table:
Query
- If
input_format_defaults_for_omitted_fields = 0, then the default value forxandaequals0(as the default value for theUInt32data type). - If
input_format_defaults_for_omitted_fields = 1, then the default value forxequals0, but the default value ofaequalsx * 2.
When inserting data with
input_format_defaults_for_omitted_fields = 1, ClickHouse consumes more computational resources, compared to insertion with input_format_defaults_for_omitted_fields = 0.Selecting data
Consider theUserActivity table as an example:
SELECT * FROM UserActivity FORMAT JSONEachRow returns:
JSON.
Any set of bytes can be output in the strings. Use the
JSONEachRow format if you are sure that the data in the table can be formatted as JSON without losing any information.Usage of Nested Structures
If you have a table with theNested data type columns, you can insert JSON data with the same structure. Enable this feature with the input_format_import_nested_json setting.
For example, consider the following table:
Query
Nested data type description, ClickHouse treats each component of the nested structure as a separate column (n.s and n.i for our table). You can insert data in the following way:
Query
input_format_import_nested_json=1.
Query
Response
Query
Response
Query
Response