【问题标题】:Convert dynamic JSON format to CSV using ChoETL使用 ChoETL 将动态 JSON 格式转换为 CSV
【发布时间】:2020-07-28 17:18:21
【问题描述】:

我会收到各种格式和层次结构的 JSON,需要知道通过动态形成列将任何 JSON 格式转换为 CSV 的可能性。例如,我提供了所需的详细信息,以下是多级 JSON以及预期的 CSV 输出

JSON

{
   "getUsers":[
      {
         "UserInformation":{
            "Id":1111122,
            "firstName":"*****1",
            "UserType":{
               "name":"CP"
            },
            "primaryState":"MA",
            "otherState":[
               "MA",
               "BA"
            ],
            "createdAt":null
         }
      },
      {
         "UserInformation":{
            "Id":3333,
            "firstName":"*****3",
            "UserType":{
               "name":"CPP"
            },
            "primaryState":"MPA",
            "otherState":[
               "KL",
               "TN"
            ],
            "createdAt":null
         }
      }
   ]
}

CSV 输出

【问题讨论】:

    标签: c# asp.net choetl


    【解决方案1】:

    这样一个可行的代码:

    读取 json 字符串并写入 CSV 文件

     StringBuilder sb = new StringBuilder();
                sb.Append(@"
    {
       ""getUsers"":[
          {
             ""UserInformation"":{
                ""Id"":1111122,
                ""firstName"":""*****1"",
                ""UserType"":{
                   ""name"":""CP""
                },
                ""primaryState"":""MA"",
                ""otherState"":[
                   ""MA"",
                   ""BA""
                ],
                ""createdAt"":null
             }
          },
          {
             ""UserInformation"":{
                ""Id"":3333,
                ""firstName"":""*****3"",
                ""UserType"":{
                   ""name"":""CPP""
                },
                ""primaryState"":""MPA"",
                ""otherState"":[
                   ""KL"",
                   ""TN""
                ],
                ""createdAt"":null
             }
          }
       ]
    }
    ");
    
    var reader = ChoJSONReader<User>.LoadText(sb.ToString());
    var user = reader.Read();
    
    using (var parser = new ChoCSVWriter<UserInformation>("User.csv").WithFirstLineHeader())
    {
        parser.Write(user.getUsers.Select(x => x.UserInformation));
    }
    

    序列化类

    class User
    {
        public List<UserDetail> getUsers { get; set; }
    }
    
    class UserDetail
    {
        public UserInformation UserInformation { get; set; }
    }
    class UserInformation
    {
        public int Id { get; set; }
        public string firstName { get; set; }
        public UserType UserType { get; set; }
        public string primaryState { get; set; }
        public List<string> otherState { get; set; }
        public DateTime? createdAt { get; set; }
    
    }
    
    class UserType
    {
        public string name { get; set; }
    }
    

    编辑

    仅使用 JSON 阅读器动态解析所有格式是不可能的。您需要一些棘手的步骤来解析它。

    1. 获取一个 json 字符串并将其最小化。
    2. 使用正则表达式什么的,认清json模式是什么。
    3. 使用该模式获取正确的模型类以对其进行反序列化。

    【讨论】:

    • 嗨@Li-Jyu Gao 谢谢你的回答,我们已经尝试过这个选项。但是我们有不同类型的 JSON 格式进来。如何动态转换为 CSV 代码,是否有可能在不使用模型对象的情况下将复杂的 json 转换为 CSV?
    • 没有一种直接的方法可以解析所有动态 json 格式。我有一种正在编辑的解决方案。
    【解决方案2】:

    以下是您如何以编程方式使用 Cinchoo ETL 完成此操作。

    首先构造配置对象,以及运行时的字段配置,并将其传递给阅读器以加载 JSON

    StringBuilder csv = new StringBuilder();
    
    var config = new ChoJSONRecordConfiguration();
    config.JSONPath = "$..getUsers[*].UserInformation";
    config.AllowComplexJSONPath = true;
    
    config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("Id"));
    config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("FirstName"));
    config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("UserType", "$.UserType.name"));
    config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("primaryState"));
    config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("otherState", "$.otherState[*]") { FieldType = typeof(string[]) });
    config.JSONRecordFieldConfigurations.Add(new ChoJSONRecordFieldConfiguration("createdAt"));
    
    using (var r = ChoJSONReader.LoadText(json, config))
    {
        using (var w = new ChoCSVWriter(csv).WithFirstLineHeader()
            .UseNestedKeyFormat(false)
            )
            w.Write(r);
    }
    
    Console.WriteLine(csv.ToString());
    

    小提琴:https://dotnetfiddle.net/Gfw3r7

    【讨论】:

    • 尝试使用上面提供的代码进行测试,但它给出了错误“从 JsonReader 读取 JArray 时出错。路径'',第 0 行,位置 0。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2015-03-26
    • 2020-12-02
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    相关资源
    最近更新 更多