Skip to content

Instantly share code, notes, and snippets.

@schmohlio
Created April 29, 2016 03:38
Show Gist options
  • Save schmohlio/6c8b50816794615714d9a6b10fd432a2 to your computer and use it in GitHub Desktop.
Save schmohlio/6c8b50816794615714d9a6b10fd432a2 to your computer and use it in GitHub Desktop.
flatten Json to arrays and primitive types
open Newtonsoft.Json
let rec private _flattenJson (delim:string) (pkey:string) (json:JsonValue): (string * JsonValue) seq =
json.Properties
|> Seq.collect (fun (k, v) ->
match v with
| JsonValue.Record r -> _flattenJson delim k (JsonValue.Record r)
| _ -> [(pkey + delim + k, v)] |> Seq.ofList
)
/// flatten a Json nested children and stop at primitive or Array. specify delimiter for nested key names.
let flattenJson delimiter (json: JsonValue) =
_flattenJson delimiter "" json
|> Seq.map (fun (k, v) -> k, v.ToString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment