Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate JsonProvider with Deedle #14

Closed
tpetricek opened this issue Feb 26, 2014 · 5 comments
Closed

Integrate JsonProvider with Deedle #14

tpetricek opened this issue Feb 26, 2014 · 5 comments

Comments

@tpetricek
Copy link
Member

It should be possible to use Frame.expandAllCols on JsonValue to flatten json values into columns. Something like this (but this does not quite work):

Frame.CustomExpanders.Add(typeof<FSharp.Data.Runtime.JsonDocument>, fun o -> seq {
  let doc = o :?> FSharp.Data.Runtime.JsonDocument
  match doc.JsonValue with 
  | JsonValue.Object(map) ->
      for KeyValue(k,v) in map do yield k,typeof<JsonValue>,box v 
  | _ -> () })
Frame.CustomExpanders.Add(typeof<JsonValue>, fun o -> seq {
  let doc = o :?> JsonValue
  match doc with 
  | JsonValue.Object(map) ->
      for KeyValue(k,v) in map do yield k,typeof<JsonValue>,box v 
  | _ -> () })

[ for l in weatherHistory.List -> series [ "It" => l ] ]
|> Frame.ofRowsOrdinal
|> Frame.expandAllCols 10
@ovatsus
Copy link
Contributor

ovatsus commented Feb 26, 2014

👍

@jrwdexter
Copy link

This almost works, but forces full expansion of the JSON record (due to recursion):

let rec expander key value =
    seq {
        match value with
        | JsonValue.String  (s) -> yield key,typeof<string>,box s
        | JsonValue.Boolean (b) -> yield key,typeof<bool>,box b
        | JsonValue.Float   (f) -> yield key,typeof<float>,box f
        | JsonValue.Null    (_) -> yield key,typeof<obj>,box ()
        | JsonValue.Number  (n) -> yield key,typeof<decimal>,box n
        | JsonValue.Record  (r) -> yield! r |> Seq.collect ((<||)expander)
        | JsonValue.Array   (a) ->
            yield! a
            |> Seq.collect (expander "arrayItem")
    }

Frame.CustomExpanders.Add(typeof<JsonDocument>, fun o -> (o :?> JsonDocument).JsonValue |> expander "root")
Frame.CustomExpanders.Add(typeof<JsonValue>, fun o ->  o :?> JsonValue |> expander "root")

// Test
[ for l in weatherHistory.List -> series [ "It" => l ] ]
|> Frame.ofRowsOrdinal
|> Frame.expandAllCols 10

@tpetricek
Copy link
Member Author

@mandest I completely forgot about this. Do you know what the issue was with my original attempt? (Or was the only issue the fact that it only handled records?)

@jrwdexter
Copy link

@tpetricek I honestly am not 100% sure. If you handle just the record cases, many more items get expanded (eg. .IsNumber). By handling all of the cases, this sort of behavior is avoided. That being said, my example above forces complete expansion, as the expander itself is recursive (array & record call the expander). I was hoping to explore this further at some point, but I haven't gotten to it.

@dsyme
Copy link
Member

dsyme commented Dec 10, 2020

FsLab is now changing to be an incubation space for F# data science projects, per discussion in #137 (comment)

Closing this out as it relates to the old FsLab package collection.

@dsyme dsyme closed this as completed Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants