使用 Cinchoo ETL 将 Microsoft Graph JSON 转换为 CSV
Converting a Microsoft Graph JSON to CSV using Cinchoo ETL
我正在尝试将 JSON 文件 ( Microsoft.Graph.Event ) 转换为 CSV 文件。我正在使用 Cinchoo ETL 来执行此操作。
这是我指的 URL:
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
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());
我正在尝试将 JSON 文件 ( Microsoft.Graph.Event ) 转换为 CSV 文件。我正在使用 Cinchoo ETL 来执行此操作。 这是我指的 URL:
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"
},
给你,我从
中提取了样本JSONhttps://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());