---
schema_version: "1"
id: edb3ffe134fb242dde6a6a15853737638c1b2baf8df49c3a4ffdfda79c4d361b
title: Reusing Types from a JSON Schema
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.12/dataweave-type-reuse-json-schema.md
canonical_url: https://docs.mulesoft.com/dataweave/2.12/dataweave-type-reuse-json-schema
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2f9b5e67856852184a893edacb54df5ad107bada82fd69206234e19f88203f41
description: MuleSoft Documentation Site
release: "2.12"
latest: false
---
---
product: DataWeave
version: 2.12
---

# Reusing Types from a JSON Schema

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

JSON Schema is a standard that provides a format for the JSON data required for a given application
and how to interact with it. Many existing data formats or types are expressed using this standard.
In particular, REST APIs described by RAML files are likely to have their types defined in JSON
schemas.

To reuse predefined types, you can load the JSON schema into DataWeave to make the types structure
available for use in your scripts.

The JSON schema loader parses schemas using the
[JSON Schema Draft 7ˆ](https://json-schema.org/specification-links#draft-7).

## Import a Type from a JSON Schema

The following example shows a JSON schema (`Person.json`) in the resources directory:

JSON Schema (`example/schema/Person.json`):

```JSON
{
  "$id": "https://example.com/person.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years, which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    }
  },
  "required": ["firstName", "lastName"]
}
```

The following example shows how to import the type using the JSON schema loader:

DataWeave Script Header:

import \* from jsonschema!example::schema::Person

## Import Syntax

To import JSON schema types, use the following syntax, where:

- `typeToImport`: You can use `*` alone to import all types defined in the schema or you can import
  a single type from the schema using, for example, `Root`. You can also import JSON schema types
  with a different name, for example, `Root as Person`, which enables you to reference the type with
  that name in the script.

- `pathToJsonSchema`: To specify the path to the schema file, replace the file separators with `::`
  and remove the `.json` extension from the file name. For example, if the path to the schema is
  `example/schema/Person.json`, use `example::schema::Person`.

import \_typesToImport\_ from jsonschema!\_pathToJsonSchema\_

The following example shows how to import a type:

import \* from jsonschema!example::schema::Person

## Use Your Types in a DataWeave Script

Including the import directive from the above example in the script header loads all the existing
types in the JSON schema. In `import * from jsonschema!example::schema::Person`, the only existing
type is the `Root` type, specified at the root which describes an Object that can have three
properties (`firstName`, `lastName`, and `age`). This is equivalent to declaring the following type
in your DataWeave script:

DataWeave Script:

```dataweave
%dw 2.0
type Root = { firstName: String, lastName: String, age?: Number }
```

Notice that `age` is the only optional field, as indicated by the `?`.

You can use the type to determine if a value follows the structure defined by the JSON schema. The
following example outputs the value `true` because the object contains the required fields,
`firstName` and `lastName`:

DataWeave Script:

```dataweave
%dw 2.0
import * from jsonschema!example::schema::Person
---
{
 firstName: "John",
 lastName: "Doe"
} is Root
```

Output:

"true"

The following example outputs the value `false` because the object doesn’t contain the required
fields, `firstName` and `lastName`:

DataWeave Script:

```dataweave
%dw 2.0
import * from jsonschema!example::schema::Person
---
{
 firstName: "John",
 age: 20
} is Root
```

Output:

"false"

## Use Definitions inside Schemas

DataWave loads the definitions inside JSON schemas when the module is imported:

JSON Schema (`example/schema/Account.json`):

```JSON
{
 "$schema": "http://json-schema.org/draft-04/schema#",
 "definitions": {
   "address": {
     "type": "object",
     "properties": {
       "street_address": {
         "type": "string"
       },
       "city": {
         "type": "string"
       },
       "state": {
         "type": "string"
       }
     },
     "required": [
       "street_address",
       "city",
       "state"
     ]
   },
   "person": {
     "type": "object",
     "properties": {
       "firstName": {
         "type": "string",
         "description": "The person's first name."
       },
       "lastName": {
         "type": "string",
         "description": "The person's last name."
       },
       "age": {
         "description": "Age in years, which must be equal to or greater than zero.",
         "type": "integer",
         "minimum": 0
       }
     },
     "required": [
       "firstName",
       "lastName"
     ]
   }
 },
 "type": "object",
 "properties": {
   "person": {
     "$ref": "#/definitions/person"
   },
   "address": {
     "$ref": "#/definitions/address"
   }
 }
}
```

You can import the types from that schema with the following directive:

import \* from jsonschema!example::schema::Account

The types defined in the schema have the same effect as declaring the following types:

type Root = { address?: address, person?: person }

type address = { street\_address: String, city: String, state: String }

type person = { firstName: String, lastName: String, age?: Number }

Use the import directive to import a single type from the schema:

import address from jsonschema!example::schema::Account

To avoid type-name collision, you can use the `as` keyword to change the imported type name to
another name:

import address as Account\_Address from jsonschema!example::schema::Account

## Facilitate Usage with Pattern Matching

You can use the types imported from the JSON schemas to take a specific action when an input is of a
certain type. These imported types are useful for pattern matching.

The following example outputs the value `person` because the object is importing that type
definition:

DataWeave Script:

```dataweave
%dw 2.0
import * from jsonschema!example::schema::Account
---
{
 firstName: "John",
 lastName: "Doe"
} match {
case is person -> "PERSON"
case is address -> "ADDRESS"
else -> "NO TYPE MATCHED"
}
```

Output:

"PERSON"

The following example outputs the value `address` because the object is importing that type
definition:

DataWeave Script:

```dataweave
%dw 2.0
import * from jsonschema!example::schema::Account
---
{
 street_address: "742 Evergreen Terrace",
 city: "Gotham",
 State: "OH"
} match {
case is person -> "PERSON"
case is address -> "ADDRESS"
else -> "NO TYPE MATCHED"
}
```

Output:

"ADDRESS"

### Built-in JSON Schema Types

DataWeave supports the following JSON schema types transformed into DataWeave types:

<table><colgroup><col> <col></colgroup><thead><tr><th>JSON Schema Type</th><th>DataWeave Type</th></tr></thead><tbody><tr><td><p><code>number</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>integer</code></p></td><td><p><code>Number</code></p></td></tr><tr><td><p><code>string</code></p></td><td><p><code>String</code></p></td></tr><tr><td><p><code>boolean</code></p></td><td><p><code>Boolean</code></p></td></tr><tr><td><p><code>array</code></p></td><td><p><code>Array</code></p></td></tr><tr><td><p><code>enums</code></p></td><td><p><code>Union</code></p></td></tr><tr><td><p><code>null</code></p></td><td><p><code>Null</code></p></td></tr><tr><td><p><code>object</code></p></td><td><p><code>Object</code></p></td></tr></tbody></table>

### Limitations

- DataWeave doesn’t support formats on JSON schema string types and transforms these types to
  `String` without any validation or restriction.

- DataWeave doesn’t support restrictions on number types such as maximums or minimums and transforms
  these types to `Number` without any validation or restriction.

- DataWeave doesn’t support patterns.

### Object types

DataWeave marks non-required properties in JSON Schema object types as optional properties in the
DataWeave Object type.

The following example outputs a DataWeave type:

JSON schema:

```JSON
{
  "type": "object",
  "properties": {
    "name":      { "type": "string" },
    "email":     { "type": "string" },
    "address":   { "type": "string" },
    "telephone": { "type": "string" }
  },
  "required": ["name", "email"]
}
```

Output:

```dataweave
%dw 2.0
type Root = { address?: String, name: String, telephone?: String, email: String }
```

If additional properties are allowed, DataWeave produces an open Object definition.

The following example outputs a DataWeave type:

JSON schema:

```JSON
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "number":      { "type": "number" },
    "street_name": { "type": "string" },
    "street_type": { "type": "string",
      "enum": ["Street", "Avenue", "Boulevard"]
    }
  },
  "required": ["number"],
  "additionalProperties": true
}
```

Output:

```dataweave
%dw 2.0
type Root = { number: Number, street_type?: "Street" | "Avenue" | "Boulevard", street_name?: String }
```

If, on the contrary, additional properties aren’t allowed, DataWeave produces a closed Object
definition.

The following example outputs a DataWeave type:

JSON schema:

```JSON
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "number":      { "type": "number" },
    "street_name": { "type": "string" },
    "street_type": { "type": "string",
      "enum": ["Street", "Avenue", "Boulevard"]
    }
  },
  "required": ["number"],
  "additionalProperties": false
}
```

Output:

```dataweave
%dw 2.0
type Root = {| number: Number, street_type?: "Street" | "Avenue" | "Boulevard", street_name?: String |}
```

### Built-in JSON Schema Support for Subschema Keywords

Using subschema keywords in JSON schema produces complex types based on the provided subschemas.

<table><colgroup><col> <col></colgroup><thead><tr><th>JSON Schema Keyword</th><th>DataWeave Type</th></tr></thead><tbody><tr><td><p><code>allOf</code></p></td><td><p><code>Intersection</code></p></td></tr><tr><td><p><code>anyOf</code></p></td><td><p><code>Union</code></p></td></tr><tr><td><p><code>oneOf</code></p></td><td><p><code>Union</code></p></td></tr></tbody></table>

The following example outputs a DataWeave type: .JSON schema:

```JSON
{
  "anyOf": [
    { "type": "string" },
    { "type": "number" }
  ]
}
```

Output:

```dataweave
%dw 2.0
type Root = (String | Number)
```
