You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 15:27:02