Pipedream中嵌套JSON特定格式name值提取问题求助
问题:提取特定格式的
name值时触发TypeError错误 我是编程新手,在Pipedream中搭建了XML转JSON的工作流,需要提取某层级下所有格式为123456-123-1234的name值。但数据结构存在差异:部分层级是单个item对象,部分是item数组,现有Node.js代码运行时出现TypeError: Cannot read property 'item' of undefined错误,相关信息如下:
完整JSON数组
[{"_attributes":{"category":"type","description":"Chassi","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"false","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"24736.00","name":"chassi","netPrice":"11625.92","qty":"1.00","salesPrice":"12615.36"},"items":{"item":{"_attributes":{"category":"type","category2":"type","description":"L06 AC : 0,6t","factoryDiscount":"chassi","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"24736.00","name":"117101-1100-005","netPrice":"11625.92","qty":"1.00","salesPrice":"12615.36"}}}},{"_attributes":{"category":"type","description":"Ancillaries","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"false","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"152.00","name":"ancilleries","netPrice":"71.44","qty":"1.00","salesPrice":"77.52"},"items":{"item":[{"_attributes":{"category":"wheels","category2":"wheels","description":"DW polyurethane with tread","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"152.00","name":"115202-1640-015","netPrice":"71.44","qty":"1.00","salesPrice":"77.52"}},{"_attributes":{"category":"wheels","category2":"wheels","description":"LW Single polyurethane","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"115202-1650-005","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"mast_options","category2":"mast_options","description":"Central lever LLC","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"112000-5100-005","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"safety","category2":"safety","description":"Technical Documentation NL","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"117101-7850-040","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"safety","category2":"safety","description":"RAL 2002 Vermillion","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"117101-7900-005","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}}]}},{"_attributes":{"category":"type","category2":"type","description":"Battery & Charger","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"0.00","name":"battery_charger","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"taxes","description":"Taxes","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"false","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"0.00","name":"taxes","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"},"items":{"item":[{"_attributes":{"category":"taxes","category2":"taxes","description":"Valorlub","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"valorlub","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"taxes","category2":"taxes","description":"Recytyre cat 1A","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"recytyre_cat_1A","netPrice":"0.00","qty":"4.00","salesPrice":"0.00"}}]}}]
原Pipedream Node.js步骤代码
export default defineComponent({ async run({ steps, $ }) { // let recordb = (steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[1].items.item.length) for (let i = 0; i < steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item.length; i++) { let record = (steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item) if (record !== undefined){ console.log(record); }//if1 let lth = steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item.length; if (lth !== undefined){ //console.log(steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item.length); for (let j = 0; j < steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item.length; i++) { let recordb = (steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item[j]) if (recordb !== undefined){ console.log(recordb); }//if2 }//for2 } }//for1 //console.log(recordb); }, })
输出内容
{ _attributes: { category: 'type', category2: 'type', description: 'L06 AC : 0,6t', factoryDiscount: 'chassi', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '24736.00', name: '117101-1100-005', netPrice: '11625.92', qty: '1.00', salesPrice: '12615.36' } } 27-12-2022 14:37:39 [ { _attributes: { category: 'wheels', category2: 'wheels', description: 'DW polyurethane with tread', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '152.00', name: '115202-1640-015', netPrice: '71.44', qty: '1.00', salesPrice: '77.52' } }, { _attributes: { category: 'wheels', category2: 'wheels', description: 'LW Single polyurethane', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '115202-1650-005', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } }, { _attributes: { category: 'mast_options', category2: 'mast_options', description: 'Central lever LLC', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '112000-5100-005', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } }, { _attributes: { category: 'safety', category2: 'safety', description: 'Technical Documentation NL', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '117101-7850-040', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } }, { _attributes: { category: 'safety', category2: 'safety', description: 'RAL 2002 Vermillion', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '117101-7900-005', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } } ] 27-12-2022 14:37:39 { _attributes: { category: 'wheels', category2: 'wheels', description: 'DW polyurethane with tread', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '152.00', name: '115202-1640-015', netPrice: '71.44', qty: '1.00', salesPrice: '77.52' } }
错误信息
TypeError Cannot read property 'item' of undefined DETAILS at Object.run (file:///tmp/pdg/dist/code/6e17abcbd3b93bd1a5ed7f5b46dabc6403308251df14c794b64e5f9a74ec9378/component.mjs:17:117) at global.executeComponent (/var/task/launch_worker.js:139:53) at MessagePort.messageHandler (/var/task/launch_worker.js:598:28)
解决方案
问题根源
- 硬编码层级路径(如
attribute[4]),数据结构变动就会触发报错; - 未区分
item是单个对象还是数组,直接访问.length导致非数组对象报错; - 内层循环错误递增外层循环的
i变量,引发索引越界,访问不存在的item。
修复后的代码
采用递归遍历处理所有层级,自动适配单个item和数组结构,同时用正则精准匹配目标格式:
export default defineComponent({ async run({ steps, $ }) { // 匹配123456-123-1234格式的正则 const targetNamePattern = /^\d{6}-\d{3}-\d{4}$/; const matchedNames = []; // 递归遍历所有items的辅助函数 const traverse = (items) => { if (!items?.item) return; // 统一将单个item转为数组,简化遍历逻辑 const itemCollection = Array.isArray(items.item) ? items.item : [items.item]; for (const item of itemCollection) { // 检查当前item的name是否符合格式 const itemName = item._attributes?.name; if (itemName && targetNamePattern.test(itemName)) { matchedNames.push(itemName); } // 递归处理子层级的items if (item.items) { traverse(item.items); } } }; // 获取XML转JSON后的根数据集合 const rootElements = steps.xml_to_json.$return_value.resource.attributes.attribute; // 遍历根层级的每个元素 for (const element of rootElements) { if (element.items) { traverse(element.items); } } console.log("提取到的符合格式的name值:", matchedNames); return matchedNames; }, })
代码说明
- 用
Array.isArray()统一处理单个item和数组,避免类型错误; - 递归遍历所有嵌套的
items,确保不遗漏深层数据; - 正则表达式
/^\d{6}-\d{3}-\d{4}$/精准匹配目标格式; - 可选链操作符
?.避免访问不存在的属性时抛出错误; - 去掉硬编码索引,改为遍历所有根元素,适配更多数据结构。
内容的提问来源于stack exchange,提问作者Oli4C
相关产品推荐
相关产品推荐

