【问题标题】:Converting a Microsoft Graph JSON to CSV using Cinchoo ETL使用 Cinchoo ETL 将 Microsoft Graph JSON 转换为 CSV
【发布时间】:2021-10-08 04:10:00
【问题描述】:

我正在尝试将 JSON 文件 ( Microsoft.Graph.Event ) 转换为 CSV 文件。我正在使用 Cinchoo ETL 来执行此操作。 这是我指的网址:

https://www.codeproject.com/Articles/1193650/Cinchoo-ETL-Quick-Start-Converting-JSON-to-CSV-Fil

这是我的代码:

            using (var csv = new ChoCSVWriter(path + calendarId + ".csv").WithFirstLineHeader())
            {
                using (var json = new ChoJSONReader(path + calendarId + ".json")
                    .WithField("id")
                    .WithField("iCalUId")
                    .WithField("isAllDay")
                    .WithField("isCancelled")
                    .WithField("isOrganizer")
                    .WithField("isOnlineMeeting")
                    .WithField("onlineMeetingProvider")
                    .WithField("type")
                    .WithField("startTime", jsonPath: "$.start.dateTime")
                    .WithField("endTime", jsonPath: "$.end.dateTime")
                    .WithField("location", jsonPath: "$.location.displayname")
                    .WithField("locationType", jsonPath: "$.location.locationType")
                    .WithField("organizer", jsonPath: "$.organizer.emailAddress.name")
                    .WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
                    )
                {
                    csv.Write(json);
                }
            }

虽然我确实获得了 CSV,并且大多数标题和值都是正确的,但其中一些是奇怪的。一些标题在后面得到一个“_0”,一些值只是前一列的重复,而不是它应该是什么。 snapshot of csv file

我已经检查了我提前写出的 JSON 文件,但它们都很好。 我正在使用 .Net Core 3.1。

我只是一个初学者,非常感谢任何帮助或建议。谢谢。

编辑 添加 CSV 的快照,其中一些值只是前一列“startTime”的重复,而不是应有的值。

这是预期的 CSV 输出:

部分 JSON 文件

  "start": {
    "dateTime": "2020-05-17T00:00:00.0000000",
    "timeZone": "UTC",
    "@odata.type": "microsoft.graph.dateTimeTimeZone"
  },
  "end": {
    "dateTime": "2020-05-18T00:00:00.0000000",
    "timeZone": "UTC",
    "@odata.type": "microsoft.graph.dateTimeTimeZone"
  },
  "location": {
    "displayName": "asdfads",
    "locationType": "default",
    "uniqueId": "b0fd5377-937d-4fb2-b70a-0a696972b46c",
    "uniqueIdType": "locationStore",
    "@odata.type": "microsoft.graph.location"
  },

【问题讨论】:

  • 最好发布 JSON 文件的 sn-p。根据您的代码,看起来某些属性指向 JSON 数组值,这些值使用后缀 _0、_1 等输出它们。可能您需要更改 JSONPath,如 .WithField("startTime", jsonPath: "$.start.dateTime[0]") 以更正它?
  • 或者试试.WithField("startTime", jsonPath: "$.start.dateTime", isArray: false)
  • TY 快速响应。这是完整 JSON 模式 link 的链接。您的我们的代码之一.WithField("startTime", jsonPath: "$.start.dateTime", isArray: false) 适用于后缀 _0,而 .WithField("startTime", jsonPath: "$.start.dateTime[0]") 没有。还有一个错误,当我更改列的顺序时,我仍然会遇到这个错误,CSV 中的一些值只是成为前一列的重复。
  • 您能否发布预期的 CSV 输出?

标签: asp.net-core serialization microsoft-graph-api choetl


【解决方案1】:

给你,我从

中提取了一个示例 JSON

https://docs.microsoft.com/en-us/graph/api/calendar-post-events?view=graph-rest-1.0&tabs=http

用于测试。

这是代码,使用 ChoETL v1.2.0.2(最新):

StringBuilder csv = new StringBuilder();

using (var w = new ChoCSVWriter(csv)
    .WithFirstLineHeader()
    )
{
    using (var r = new ChoJSONReader(@"*** YOUR GRAPH JSON FILE PATH ***")
        .WithField("id")
        .WithField("iCalUId")
        .WithField("isAllDay")
        .WithField("isCancelled")
        .WithField("isOrganizer")
        .WithField("isOnlineMeeting")
        .WithField("onlineMeetingProvider")
        .WithField("type")
        .WithField("startTime", jsonPath: "$.start.dateTime", isArray: false)
        .WithField("endTime", jsonPath: "$.end.dateTime", isArray: false)
        .WithField("location", jsonPath: "$.location.displayname")
        .WithField("locationType", jsonPath: "$.location.locationType", isArray: false)
        .WithField("organizer", jsonPath: "$.organizer.emailAddress.name", isArray: false)
        .WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
    )
    {
        w.Write(r);
    }
}

Console.WriteLine(csv.ToString());

输出:

id,iCalUId,isAllDay,isCancelled,isOrganizer,isOnlineMeeting,onlineMeetingProvider,type,startTime,endTime,location,locationType,organizer,recurrence
AAMkAGViNDU7zAAAAA7zAAAZb2ckAAA=,040000008200E641B4C,False,False,True,False,unknown,singleInstance,3/15/2019 12:00:00 PM,3/15/2019 2:00:00 PM,,default,Megan Bowen,

更新: 这是用于更改字段顺序并获取参加者人数的更新代码

StringBuilder csv = new StringBuilder();

using (var w = new ChoCSVWriter(csv)
    .WithFirstLineHeader()
    )
{
    using (var r = new ChoJSONReader(@"*** YOUR GRAPH JSON FILE PATH ***")
        .WithField("startTime", jsonPath: "$.start.dateTime", isArray: false)
        .WithField("endTime", jsonPath: "$.end.dateTime", isArray: false)
        .WithField("id")
        .WithField("iCalUId")
        .WithField("isAllDay")
        .WithField("isCancelled")
        .WithField("isOrganizer")
        .WithField("isOnlineMeeting")
        .WithField("onlineMeetingProvider")
        .WithField("type")
        .WithField("location", jsonPath: "$.location.displayname")
        .WithField("locationType", jsonPath: "$.location.locationType", isArray: false)
        .WithField("organizer", jsonPath: "$.organizer.emailAddress.name", isArray: false)
        .WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
        .WithField("attendees", jsonPath: "$.attendees[*]", valueConverter: o => ((IList)o).Count)
    )
    {
        w.Write(r);
    }
}

Console.WriteLine(csv.ToString());

【讨论】:

  • 感谢@RajN 的快速解答。之后让我检查一下这段代码。
  • 您的代码有效,我已经接受了答案!不过,我还有一些其他问题,你能不改变列的顺序吗?例如)将 startTime 和 endTime 向左移动。而且,您如何处理无法确定将有多少个数组的数组值?例如)“与会者”:[]。我只需要每个 JSON 的参与者总数。
猜你喜欢
  • 2021-07-20
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 2021-08-29
相关资源
最近更新 更多