使用ChoETL将嵌套JSON转换为CSV时合并多级字段的问题求助
解决方案
你不需要分两次读取JSON,直接一次性读取根节点后展开嵌套数组,合并所有层级的字段即可,完整实现代码如下:
using (var jsonReader = new ChoJSONReader(inBlob).WithJSONPath("$").AllowComplexJSONPath(true)) { var outputData = jsonReader.SelectMany(root => // 展开外层order数组 root.order.SelectMany(order => // 展开每个订单下的商品数组 ((dynamic[])order.OrderArticles).Select(article => new { // 根层级公共字段 created = root.system.created, by = root.system.by, id = root.location.id, country = root.location.country, // 订单层级字段 OrderID = order.OrderID, OrderName = order.OrderName, // 商品层级字段 Size = article.Size, ProductName = article.ProductName, ProductId = article.ProductId }) ) ); // 直接输出为分号分隔的CSV文件 using (var csvWriter = new ChoCSVWriter("你的输出路径.csv").WithDelimiter(";").WithFirstLineHeader()) { csvWriter.Write(outputData); } return outputData; }
注意事项
你原有代码里存在笔误,把OrderArticles写成了OutputArticles,修正后即可正常读取商品层级字段。
内容的提问来源于stack exchange,提问作者Cokeisit
相关产品推荐
相关产品推荐

