JSONWriter writes data in the .
JSONWriter
JSON file
1-n
0-1
✓
x
x
x
x
x
x
Input
0-N
At least one
Input records to be joined and mapped into the JSON structure.
Any (each port can have different metadata)
Output
0
x
Optional. For port writing.
JSONWriter does not propagate metadata.
JSONWriter has no metadata template.
Basic
File URL
yes
Charset
The encoding of an output file generated by JSONWriter.
The default encoding depends on DEFAULT_SOURCE_CODE_CHARSET
in defaultProperties.
UTF-8 | <other encodings>
Mapping
k:p
Defines how input data is mapped onto an output JSON. See Details below.
Mapping URL
k:p
External text file containing the mapping definition.
Advanced
Create directories
By default, non-existing directories are not created. If set to true
, they are created.
false (default) | true
Omit new lines wherever possible
By default, each element is written to a separate line. If set to true
, new lines are omitted when writing data to the output JSON structure. Thus, all JSON elements are on one line only.
false (default) | true
Cache size
The size of the database used when caching data from ports to elements (the data is first processed then written). The larger your data is, the larger cache is needed to maintain fast processing.
auto (default) | e.g. 300MB, 1GB etc.
Cache in Memory
Cache data records in memory instead of the JDBM’s disk cache (default). Note that while it is possible to set the maximal size of the disk cache, this setting is ignored in case the in-memory cache is used. As a result, an OutOfMemoryError
may occur when caching too many data records.
false (default) | true
Sorted input
Tells JSONWriter whether the input data is sorted. Setting the attribute to true declares you want to use the sort order defined in Sort keys, see below.
false (default) | true
Sort keys
Max number of records
0-N
Partitioning
Records per file
1-N
Partition key
Partition lookup table
Partition file tag
Number file tag (default) | Key file tag
Partition output fields
Partition unassigned file name
Partition key sorted
false (default) | true
Create empty files
If set to false
, prevents the component from creating an empty output file when there are no input records.
true (default) | false
[1] One of these has to be specified. If both are specified, Mapping URL has a higher priority.
JSONWriter receives data from all connected input ports and converts records to JSON objects based on the mapping you define. Finally, the component writes the resulting tree structure of elements to the output: a JSON file, port or dictionary. JSONWriter can write lists and variants.
Every JSON object can contain other nested JSON objects. Thus, the JSON format resembles XML and similar tree formats.
Connect input edges to JSONWriter and edit the component’s Mapping attribute. This will open the visual mapping editor.
Metadata on the input edge(s) are displayed on the left-hand side. The right-hand pane is where you design the desired JSON tree. Mapping is then performed by dragging metadata from left to right (and performing additional tasks described below).
In the right hand pane, design your JSON tree consisting of:
Arrays - arrays are ordered sets of values in JSON enclosed between the [ and ] brackets. To learn how to map them in JSONWriter, see Writing arrays II below.
Connect input records to output (wildcard) elements to create Binding.
Example 26. Creating Binding Example mapping in JSONWriter - employees are joined with projects they work on. Fields in bold (their content) will be printed to the output file - see below.
Excerpt from the output file related to the figure above (example of one employee written as JSON):
When writing variant fields, JSONWriter translates the tree structure of variant directly to JSON. A variant map is formatted as JSON object, variant list is formatted as JSON array. Dates in variant are formatted as datetime strings in UTC. Byte arrays in variant are formatted as Base64 strings.
This example shows a way to write flat records (no arrays, no subtrees) to a JSON file.
The input edge connected to JSONWriter has metadata fields CommodityName, Unit, Price and Currency and receives the data:
Write the data to a JSON file.
Solution
Set up the File URL and Mapping attributes.
File URL
${DATAOUT_DIR}/comodities.json
Mapping
Produced JSON File
This examples shows a way to write arrays.
The input edge connected to the JSONWriter has metadata fields CommodityName, Unit, Price and Currency. It is similar to the previous example, but the price is not a single value but a list of values.
Solution
Set up the File URL and Mapping attributes.
File URL
${DATAOUT_DIR}/comodities2.json
Mapping
Produced JSON File
This example shows a way to write summary array using values of all input records.
Set up the File URL and Mapping attributes.
Solution
File URL
${DATAOUT_DIR}/comodities3.json
Mapping
Produced JSON File
This example shows a way to use wild cards to map input metadata fields.
Write the data from the first example to a JSON file. The solution must be flexible - it must propagate the changes in input metadata to the output without changing the configuration of JSONWriter.
Solution
File URL
${DATAOUT_DIR}/comodities4.json
Mapping
Produced JSON File
This example shows a way to write output elements names based on input data.
Write the data from the first example to a JSON file. The name of the element containing the price of commodity should be the unit of measurement.
Solution
File URL
${DATAOUT_DIR}/comodities5.json
Mapping
Notice the dummy element CommodityName
which you bind the input field to.
Produced JSON File
This example shows a way to merge data from multiple input edges to a JSON file.
There are two input edges. The records on the first one contain a commodity name and unit of measurement. The records on the second one contain a commodity name, price per unit and currency. Multiple records from the second input port can correspond to a single record from the first input port. Create a JSON file which contains record from the first input port and corresponding records from the second output port as a subtree.
Solution
File URL
${DATAOUT_DIR}/comodities6.json
Mapping
Produced JSON File
We recommend users explicitly specify Charset.
Only one field (byte
, cbyte
or string
) is used. The field name is used in File URL to govern how the output records are processed - see
The target file for the output JSON. See .
Tells JSONWriter how the input data is sorted, thus enabling streaming. The sort order of fields can be given for each port in a separate tab. Working with Sort keys has been described in .
The maximum number of records written to all output files. See .
The maximum number of records that are written to a single file. See .
The key whose values control the distribution of records among multiple output files. See .
The ID of a lookup table. The table serves for selecting records which should be written to the output file(s). See .
By default, output files are numbered. If this attribute is set to Key file tag
, output files are named according to the values of Partition key or Partition output fields. See .
The fields of Partition lookup table whose values serve for naming output file(s). See .
The name of a file that the unassigned records should be written into (if there are any). If it is not given, the data records whose key values are not contained in Partition lookup table are discarded. See .
In case partitioning into multiple output files is turned on, all output files are open at once. This could lead to an undesirable memory footprint for many output files (thousands). Moreover, for example unix-based OS usually have a very strict limitation of number of simultaneously open files (1,024) per process. In case you run into one of these limitations, consider sorting the data according a partition key using one of our standard sorting components and set this attribute to true. The partitioning algorithm does not need to keep open all output files, just the last one is open at one time. See .
As a consequence, you map the input records to the output file in a manner similar to . Mapping editors in both components have similar logic. The very basics of mapping are:
WARNING Unlike XMLWriter , you do not map metadata to any attributes.
- another option to mapping elements explicitly. You use the Include and Exclude patterns to generate element names from respective metadata.
At any time, you can switch to the and write/check the mapping yourself in code.
If the basic instructions found here are not satisfying, please consult XMLWriter’s where the whole mapping process is described in detail.