Contact Free trial Login

read

read(String | Binary, String, Object)

Reads the input string or binary and returns parsed content.

This function can be useful if the reader cannot determine the content type by default.

Parameters

Name Description

stringToParse

The string or binary to read.

contentType

A supported format (or content type). Default: application/dw.

readerProperties

Optional: Sets reader configuration properties. For other formats and reader configuration properties, see DataWeave Output Formats and Writer Properties.

Example

This example reads a string as a CSV format without a header and outputs it to JSON. Notice that is adds column names as keys to the output object. Also, if you do not append [0] to the function call, the results will return as an array (with square brackets surrounding the entire output object).

Source

%dw 2.0
var myVar = "Some, Body"
output application/json
---
read(myVar,"application/csv",{header:false})[0]

Output

{
  "column_0": "Some",
  "column_1": " Body"
}

Example

This example reads the input XML and shows the syntax for a reader property.

Source

%dw 2.0
output application/xml
---
{
   "XML" : read("<prices><basic>9.99</basic></prices>",
                "application/xml",
                { indexedReader: "false" })."prices"
}

Output

<?xml version='1.0' encoding='UTF-8'?>
<XML>
  <basic>9.99</basic>
</XML>

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub