Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(xml): keep leading zeros in Convert XML to JSON (#15856)

Odai Ahmade44f97ef6334
diff --git a/bun.lock b/bun.lock
index ab116368a67..769cf0e35b8 100644
--- a/bun.lock
+++ b/bun.lock
@@ -10731,7 +10731,7 @@
},
"packages/pieces/core/xml": {
"name": "@activepieces/piece-xml",
- "version": "0.2.0",
+ "version": "0.2.1",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
diff --git a/packages/pieces/core/xml/package.json b/packages/pieces/core/xml/package.json
index e1c51d54cdb..db2c711f3eb 100644
--- a/packages/pieces/core/xml/package.json
+++ b/packages/pieces/core/xml/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-xml",
- "version": "0.2.0",
+ "version": "0.2.1",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/core/xml/src/i18n/translation.json b/packages/pieces/core/xml/src/i18n/translation.json
index 57c7167bf59..87a4f5ece1b 100644
--- a/packages/pieces/core/xml/src/i18n/translation.json
+++ b/packages/pieces/core/xml/src/i18n/translation.json
@@ -29,5 +29,7 @@
"Write empty values as <tag/> instead of <tag></tag>.": "Write empty values as <tag/> instead of <tag></tag>.",
"Keys starting with this prefix become attributes. Defaults to \"@_\", the prefix Convert XML to JSON uses.": "Keys starting with this prefix become attributes. Defaults to \"@_\", the prefix Convert XML to JSON uses.",
"Key that holds the text of an element that also has attributes. Defaults to \"#text\".": "Key that holds the text of an element that also has attributes. Defaults to \"#text\".",
- "Key whose value is written as a CDATA section, so it is not escaped. Defaults to \"#cdata\".": "Key whose value is written as a CDATA section, so it is not escaped. Defaults to \"#cdata\"."
+ "Key whose value is written as a CDATA section, so it is not escaped. Defaults to \"#cdata\".": "Key whose value is written as a CDATA section, so it is not escaped. Defaults to \"#cdata\".",
+ "Keep Leading Zeros": "Keep Leading Zeros",
+ "Keep numbers that start with a zero as text so values like ZIP codes, IDs and account numbers are not changed (e.g. \"02134\" stays \"02134\" instead of becoming 2134). Other numbers are still converted to numbers.": "Keep numbers that start with a zero as text so values like ZIP codes, IDs and account numbers are not changed (e.g. \"02134\" stays \"02134\" instead of becoming 2134). Other numbers are still converted to numbers."
}
\ No newline at end of file
diff --git a/packages/pieces/core/xml/src/lib/actions/convert-xml-to-json.ts b/packages/pieces/core/xml/src/lib/actions/convert-xml-to-json.ts
index 489f28c1ef4..5257fbc44a5 100644
--- a/packages/pieces/core/xml/src/lib/actions/convert-xml-to-json.ts
+++ b/packages/pieces/core/xml/src/lib/actions/convert-xml-to-json.ts
@@ -8,7 +8,7 @@ export const convertXmlToJson = createAction({
displayName: 'Convert XML to JSON',
description: 'Convert XML to JSON',
aiMetadata: {
- description: 'Parses an XML string into a JSON structure, in either of two modes: keep tag attributes (surfaced as "@_"-prefixed keys) or ignore attributes and keep element text only. Use it to make an XML payload from an HTTP response, webhook body, or RSS/SOAP feed addressable by later steps; for the opposite direction use Create XML, which accepts this output as-is. Requires a well-formed XML string passed as text, and the XML declaration is always dropped; read-only and idempotent.',
+ description: 'Parses an XML string into a JSON structure, in either of two modes: keep tag attributes (surfaced as "@_"-prefixed keys) or ignore attributes and keep element text only. Numeric-looking text is converted to numbers unless Keep Leading Zeros is on, in which case zero-prefixed values such as ZIP codes and IDs stay strings. Use it to make an XML payload from an HTTP response, webhook body, or RSS/SOAP feed addressable by later steps; for the opposite direction use Create XML, which accepts this output as-is. Requires a well-formed XML string passed as text, and the XML declaration is always dropped; read-only and idempotent.',
idempotent: true,
},
props: {
@@ -23,10 +23,20 @@ export const convertXmlToJson = createAction({
required: false,
defaultValue: false,
}),
+ keepLeadingZeros: Property.Checkbox({
+ displayName: 'Keep Leading Zeros',
+ description: 'Keep numbers that start with a zero as text so values like ZIP codes, IDs and account numbers are not changed (e.g. "02134" stays "02134" instead of becoming 2134). Other numbers are still converted to numbers.',
+ required: false,
+ defaultValue: true,
+ }),
},
async run(context) {
- const { xml, ignoreAttributes } = context.propsValue;
- const parser = new XMLParser({ ignoreAttributes: ignoreAttributes ?? false, ignoreDeclaration: true });
+ const { xml, ignoreAttributes, keepLeadingZeros } = context.propsValue;
+ const parser = new XMLParser({
+ ignoreAttributes: ignoreAttributes ?? false,
+ ignoreDeclaration: true,
+ ...((keepLeadingZeros ?? false) ? { numberParseOptions: { hex: true, leadingZeros: false, eNotation: true } } : {}),
+ });
return parser.parse(xml);
},
});
diff --git a/packages/pieces/core/xml/test/convert-xml-to-json.test.ts b/packages/pieces/core/xml/test/convert-xml-to-json.test.ts
index 05be25c0e75..86401483765 100644
--- a/packages/pieces/core/xml/test/convert-xml-to-json.test.ts
+++ b/packages/pieces/core/xml/test/convert-xml-to-json.test.ts
@@ -64,3 +64,71 @@ describe('convertXmlToJson', () => {
expect(root['item']).toEqual([1, 2, 3]);
});
});
+
+describe('convertXmlToJson keepLeadingZeros', () => {
+ test('keeps leading zeros as strings when enabled', async () => {
+ const ctx = createMockActionContext({
+ propsValue: {
+ xml: '<root><zip>02134</zip><id>00123</id><count>42</count></root>',
+ ignoreAttributes: false,
+ keepLeadingZeros: true,
+ },
+ });
+ const result = await convertXmlToJson.run(ctx);
+ expect(result).toEqual({ root: { zip: '02134', id: '00123', count: 42 } });
+ });
+
+ test('keeps leading zeros in nested elements and arrays when enabled', async () => {
+ const ctx = createMockActionContext({
+ propsValue: {
+ xml: '<customers><customer><account>000987</account><address><zip>02134</zip></address></customer><customer><account>12345</account><address><zip>00501</zip></address></customer></customers>',
+ ignoreAttributes: false,
+ keepLeadingZeros: true,
+ },
+ });
+ const result = await convertXmlToJson.run(ctx);
+ expect(result).toEqual({
+ customers: {
+ customer: [
+ { account: '000987', address: { zip: '02134' } },
+ { account: 12345, address: { zip: '00501' } },
+ ],
+ },
+ });
+ });
+
+ test('keeps parsing zero, decimals and exponents as numbers when enabled', async () => {
+ const ctx = createMockActionContext({
+ propsValue: {
+ xml: '<root><a>0</a><b>0.5</b><c>1e5</c><d>007.5</d></root>',
+ ignoreAttributes: false,
+ keepLeadingZeros: true,
+ },
+ });
+ const result = await convertXmlToJson.run(ctx);
+ expect(result).toEqual({ root: { a: 0, b: 0.5, c: 100000, d: '007.5' } });
+ });
+
+ test('strips leading zeros when disabled', async () => {
+ const ctx = createMockActionContext({
+ propsValue: {
+ xml: '<root><zip>02134</zip><id>00123</id><count>42</count></root>',
+ ignoreAttributes: false,
+ keepLeadingZeros: false,
+ },
+ });
+ const result = await convertXmlToJson.run(ctx);
+ expect(result).toEqual({ root: { zip: 2134, id: 123, count: 42 } });
+ });
+
+ test('keeps the previous numeric output for existing steps without the option', async () => {
+ const ctx = createMockActionContext({
+ propsValue: {
+ xml: '<root><zip>02134</zip><item>007</item><item>8</item></root>',
+ ignoreAttributes: false,
+ },
+ });
+ const result = await convertXmlToJson.run(ctx);
+ expect(result).toEqual({ root: { zip: 2134, item: [7, 8] } });
+ });
+});