[英]F# and Web API Json for analysis in DataFrame
Im trying to learn F# by rewriting some scripts from python, where I query a graphql endpoint and load the json into a Pandas DataFrame for cleaning and analysis:
json 像这样:
apiResponse = {"data":
{"Shipments":{"ErrorMessage":null,
"Success":true,
"ValidationResult":null,
"TotalCount":494,
"Data":[
{"Building":"B7",
"Comment":"Test",
"CompletedDate":"2021-04-12T10:13:13.436Z",
"ItemId":"dd4520bb-aa0a-...",
"NoOfUnit":5,
"Organization":{
"OrganizationId":"cac43a32-1f08-...",
"OrganizationName":"XXX"},
"Adress":"Road 5"
},
{"Building":"B7",
....}]}}}
Python:
data = request.json()
#only the "Data" path
json_list = data['data']['Shipments']['Data']
df = json_normalize(json_list)
使用 Fsharp.Data 的方法相同,而“file.json”只是“Data[]”部分(我试图创建一个小提琴,但我无法让它运行。这里:
type ApiTypes = JsonProvider<"file.json"> //where file.json has only "Data" path of apiResponse
let jsonResponse = JsonValue.Parse(apiResponse)
let data = jsonResponse?data
let Shipments = data?Shipments
let Data = Shipments?Data
let input =
ApiTypes.Parse(Data.ToString())
let df = Frame.ofRecords input
但这不起作用。 所以我的问题:
任何帮助表示赞赏。 谢谢
我对 Deedle 没有任何经验,但我认为ofRecords
需要实际的 static 类型才能正常工作,因此它可能与JsonProvider
不兼容。 (至少我无法让它工作。)
相反,我会手动定义类型,然后对其进行反序列化,如下所示:
open Newtonsoft.Json.Linq
type Datum =
{
Building : string
Comment : string
CompletedDate : DateTime
ItemId : Guid
NoOfUnit : int
Organization :
{|
OrganizationId : Guid
OrganizationName : string
|}
Adress : string
}
let jobj = JObject.Parse(apiResponse)
let frame =
jobj.["data"].["Shipments"].["Data"].Children()
|> Seq.map (fun jtok -> jtok.ToObject<Datum>())
|> Frame.ofRecords
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.