---
schema_version: "1"
id: 22984e248c13f927a8740f289425bed3a19e019ef7c30e243acd251511512fd8
title: Output a Field When Present
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.4/dataweave-cookbook-output-a-field-when-present.md
canonical_url: https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-output-a-field-when-present
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 1e5a5a7f4ddc13fbfb877eb70fc88003bd994deb8d92a0fafb0163a8ee73ac2b
description: MuleSoft Documentation Site
release: "2.4"
latest: false
---
---
product: DataWeave
version: 2.4
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Output a Field When Present

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

This DataWeave example outputs a field if it is present in the input, a JSON array. The first object
in the array contains `"insurance"`, while the second does not. The XML output mirrors this
structure.

The example uses these functions:

- `map` to go through every element within the input array.

- `if($.insurance?)` to determine when to output an `insurance` field.

DataWeave Script:

```dataweave
%dw 2.0
output application/xml
---
users: { (payload map
        user: {
          name: $.name,
          (insurance: $.insurance) if($.insurance?)
        } )
}
```

Input JSON Payload:

```json
[
  {
    "name": "Julian",
    "gender": "Male",
    "age": 41,
    "insurance": "Osde"
  },
  {
    "name": "Mariano",
    "gender": "Male",
    "age": 33
  }
]
```

Output XML:

```xml
<?xml version='1.0' encoding='US-ASCII'?>
<users>
  <user>
    <name>Julian</name>
    <insurance>Osde</insurance>
  </user>
  <user>
    <name>Mariano</name>
  </user>
</users>
```

## Related Examples

- [Map Data with DataWeave](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-map)

- [Exclude Fields from the Output](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-exclude-field)

- [Insert an Attribute into an XML Tag](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-insert-attribute)

- [Conditionally Reduce a List through a Function](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-conditional-list-reduction-via-function)

- [Change Format According to Type](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-format-according-to-type)

## See Also

- [DataWeave Types](https://docs.mulesoft.com/dataweave/2.4/dataweave-types)

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.4/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook)
