Skip to main content

Task String - JSON Encode​

The JSON Encode Task turns delimited text from a file or a value into JSON, one JSON object for each line, and writes it to the output or to a file.

The Main settings, Flow, On error, Conditions and TimeOut tabs are the same for every Task; see Job - Tasks.

JSON Encode tab​

JSON Encode tab, with the Credential, the File and Variable/Value input options and the property map

  • Credentials: the Credential used to read the file when File is selected
  • File (default): the full path of a text file. Click the file button to browse for it
  • Variable/Value: the text as a value or as a Variable. Click the Variable button to insert a Variable
  • Map columns to properties (double click to edit): each row maps a JSON property (Name) to an input column (Value), with a value type (Type): "None", "Integer", "Float", "String", "Boolean", "Date", "Object" or "Array". Use a dotted path in Name for a nested property, such as Address.City, and [] after a name for an array of objects, such as InnerArray[].Value. In the Add/Edit name value window, clear The value is the name of column to enter a value or a Variable instead of a column. Use Add, Edit and Delete to change the rows. Refresh removes every row from the map

When Include field names is checked on the Input/Output tab, Value is the column name from the first line. When it is cleared, the rows of the map are matched to the columns by position: the first row to the first column, and so on. A column value that contains ; becomes an array of values, unless the value is enclosed in the text qualifier.

Without a map, each column becomes a property named after its column. This needs Include field names; without it, the objects are empty.

Input/Output tab​

Input/Output tab of the JSON Encode Task, with the input settings, the output settings and the file output settings

Input settings

  • Include field names: the first line of the input holds the column names
  • Indent XML: has no effect on this Task
  • Field separator: the character between columns, "Tab" by default
  • Text qualifier: an optional character around column values
  • Line break: the character between lines, "CrLf" by default

Output settings

  • Force JSON array output for single-line input: always writes a JSON array, even when the input has one line of values or none. Without it, one line gives a single JSON object and more lines give an array
  • Output to standard output (default): writes the JSON to the output of the Task
  • Output to file: writes the JSON to the file in File path, read and written with the Credential selected above it
    • Include BOM (if UTF8): checked by default. Writes a byte order mark when the Task code page is UTF-8
    • Append to file: checked by default. Adds the JSON to the end of an existing file, after a line break. When cleared, an existing file is replaced without warning
warning

With Append to file checked, each run adds another JSON document to the same file, and the file as a whole is no longer valid JSON. Clear Append to file to replace the file on each run.

Example​

Input, with Include field names checked (columns separated by tabs):

Column1 Column2 ArrayColumn
Value1 Value2 item1;item2;item3

With the map Property1 = Column1, Property2 = Column2 and InnerArray[].Value = ArrayColumn, the output is (shown indented; the Task writes it on one line):

{
"Property1": "Value1",
"Property2": "Value2",
"InnerArray": [
{ "Value": "item1" },
{ "Value": "item2" },
{ "Value": "item3" }
]
}

Output and errors​

warning

If the file or the value is empty, the Task writes nothing, and the Task reports success.