Contact Us 1-800-596-4880

read

read(String | Binary, String, Object): Any

Reads a 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 Supported Data Formats.

Example

This example reads a JSON object { "hello" : "world" }', and it uses the "application/json" argument to indicate input content type. By contrast, the output application/xml directive in the header of the script tells the script to transform the JSON content into XML output. Notice that the XML output uses hello as the root XML element and world as the value of that element. The hello in the XML corresponds to the key "hello" in the JSON object, and world corresponds to the JSON value "world".

Source

%dw 2.0
output application/xml
---
read('{ "hello" : "world" }','application/json')

Output

<?xml version='1.0' encoding='UTF-8'?><hello>world</hello>

Example

This example reads a string as a CSV format without a header and transforms it to JSON. Notice that it adds column names as keys to the output object. Also, it appends [0] to the function call here to select the first index of the resulting array, which avoids producing the results within 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 specified XML and shows the syntax for a reader property, in this case, { indexedReader: "false" }.

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>

Example

This example returns the input parameter if it is in XML or JSON format, otherwise, it returns the message Not XML or JSON. The example uses the read function to verify if the specified input string is in XML or JSON.

Source

%dw 2.0
import * from dw::Runtime
output application/dw
var xmlInput = "<Input><name>Max the Mule</name><ID>123</ID><Type>XML</Type></Input>"
var jsonInput = '{"Input": {"name": "Max the Mule", "ID": 123, "Type": "JSON"}}'
var stringInput = "name: Max the Mule, ID: 123, Type: String"
//This function returns the input parameter if it is in XML or JSON format, otherwise, it returns a static message
fun testType(inputMessage) =
    // Reads the input only if the data is in JSON format
    try(() -> read(inputMessage,"application/json"))
    // Reads the input only if the data is in XML format
    orElseTry(() -> read(inputMessage,"application/xml"))
    // Return this message if the input data is not either in XML or JSON format
    orElse("Not XML or JSON")
---
{
    "Test 'xmlInput' ": testType(xmlInput),
    "Test 'jsonInput' ": testType(jsonInput),
    "Test 'stringInput' ": testType(stringInput),
}

Output

{
  "Test 'xmlInput' ": {
    Input: {
      name: "Max the Mule",
      ID: "123",
      Type: "XML"
    }
  },
  "Test 'jsonInput' ": {
    Input: {
      name: "Max the Mule",
      ID: 123,
      Type: "JSON"
    }
  },
  "Test 'stringInput' ": "Not XML or JSON"
}