Skip to main content

field.convert

Convert the type of a field.

Description

Convert takes the field of one type and converts it into another type (e.g. string to integer). The applicable types are string, int, float and bool. Converting can be done between any combination of types. Note that booleans will be converted to numeric values 1 (true) and 0 (false). Processor is only applicable to .Key, .Payload.Before and .Payload.After prefixes, and only applicable if said fields contain structured data. If the record contains raw JSON data, then use the processor json.decode to parse it into structured data first.

Configuration parameters

version: 2.2
pipelines:
- id: example
status: running
connectors:
# define source and destination ...
processors:
- id: example
plugin: "field.convert"
settings:
# Field is the target field that should be converted. Note that you
# can only convert fields in structured data under `.Key` and
# `.Payload`.
# For more information about the format, see [Referencing
# fields](https://conduit.io/docs/using/processors/referencing-fields).
# Type: string
field: ""
# Whether to decode the record key using its corresponding schema from
# the schema registry.
# Type: bool
sdk.schema.decode.key.enabled: "true"
# Whether to decode the record payload using its corresponding schema
# from the schema registry.
# Type: bool
sdk.schema.decode.payload.enabled: "true"
# Whether to encode the record key using its corresponding schema from
# the schema registry.
# Type: bool
sdk.schema.encode.key.enabled: "true"
# Whether to encode the record payload using its corresponding schema
# from the schema registry.
# Type: bool
sdk.schema.encode.payload.enabled: "true"
# Type is the target field type after conversion, available options
# are: `string`, `int`, `float`, `bool`, `time`.
# Type: string
type: ""

Examples

Convert int to time

This example takes an int in field .Payload.After.createdAt and parses it as a unix timestamp into a time.Time value.

Configuration parameters

version: 2.2
pipelines:
- id: example
status: running
connectors:
# define source and destination ...
processors:
- id: example
plugin: "field.convert"
settings:
field: ".Payload.After.createdAt"
type: "time"

Record difference

Before
After
1
{
1
{
2
  "position": null,
2
  "position": null,
3
  "operation": "create",
3
  "operation": "create",
4
  "metadata": null,
4
  "metadata": null,
5
  "key": {
5
  "key": {
6
    "id": 123.345
6
    "id": 123.345
7
  },
7
  },
8
  "payload": {
8
  "payload": {
9
    "before": null,
9
    "before": null,
10
    "after": {
10
    "after": {
11
-
      "createdAt": 1704198896123456800
11
+
      "createdAt": "2024-01-02T12:34:56.123456789Z"
12
    }
12
    }
13
  }
13
  }
14
}
14
}

Convert float to string

This example takes the float in field .Key.id and changes its data type to string.

Configuration parameters

version: 2.2
pipelines:
- id: example
status: running
connectors:
# define source and destination ...
processors:
- id: example
plugin: "field.convert"
settings:
field: ".Key.id"
type: "string"

Record difference

Before
After
1
{
1
{
2
  "position": null,
2
  "position": null,
3
  "operation": "update",
3
  "operation": "update",
4
  "metadata": null,
4
  "metadata": null,
5
  "key": {
5
  "key": {
6
-
    "id": 123.345
6
+
    "id": "123.345"
7
  },
7
  },
8
  "payload": {
8
  "payload": {
9
    "before": null,
9
    "before": null,
10
    "after": {
10
    "after": {
11
      "foo": "bar"
11
      "foo": "bar"
12
    }
12
    }
13
  }
13
  }
14
}
14
}

Convert int to bool

This example takes the int in field .Payload.After.done and changes its data type to bool.

Configuration parameters

version: 2.2
pipelines:
- id: example
status: running
connectors:
# define source and destination ...
processors:
- id: example
plugin: "field.convert"
settings:
field: ".Payload.After.done"
type: "bool"

Record difference

Before
After
1
{
1
{
2
  "position": null,
2
  "position": null,
3
  "operation": "update",
3
  "operation": "update",
4
  "metadata": null,
4
  "metadata": null,
5
  "key": {
5
  "key": {
6
    "id": "123"
6
    "id": "123"
7
  },
7
  },
8
  "payload": {
8
  "payload": {
9
    "before": null,
9
    "before": null,
10
    "after": {
10
    "after": {
11
-
      "done": 1
11
+
      "done": true
12
    }
12
    }
13
  }
13
  }
14
}
14
}

Convert string to int

This example takes the string in field .Key.id and changes its data type to int.

Configuration parameters

version: 2.2
pipelines:
- id: example
status: running
connectors:
# define source and destination ...
processors:
- id: example
plugin: "field.convert"
settings:
field: ".Key.id"
type: "int"

Record difference

Before
After
1
{
1
{
2
  "position": null,
2
  "position": null,
3
  "operation": "update",
3
  "operation": "update",
4
  "metadata": null,
4
  "metadata": null,
5
  "key": {
5
  "key": {
6
-
    "id": "123"
6
+
    "id": 123
7
  },
7
  },
8
  "payload": {
8
  "payload": {
9
    "before": null,
9
    "before": null,
10
    "after": {
10
    "after": {
11
      "foo": "bar"
11
      "foo": "bar"
12
    }
12
    }
13
  }
13
  }
14
}
14
}

scarf pixel conduit-site-docs-using-processors