9 people like it.

Rename JSON properties

Rename JSON property names by reconstructing JSON object.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
open Newtonsoft.Json.Linq

let rec rename (f: string -> string) (json: JToken) =
    match json with
    | :? JProperty as prop -> 
        let name = f prop.Name
        let cont = rename f prop.Value
        new JProperty(name, cont :> obj) :> JToken
    | :? JArray as arr ->
        let cont = arr |> Seq.map (rename f)
        new JArray(cont) :> JToken
    | :? JObject as o ->
        let cont = o.Properties() |> Seq.map (rename f)
        new JObject(cont) :> JToken
    | _ -> json
namespace Newtonsoft
namespace Newtonsoft.Json
namespace Newtonsoft.Json.Linq
val rename : f:(string -> string) -> json:JToken -> JToken

Full name: Script.rename
val f : (string -> string)
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val json : JToken
type JToken =
  member AddAfterSelf : content:obj -> unit
  member AddAnnotation : annotation:obj -> unit
  member AddBeforeSelf : content:obj -> unit
  member AfterSelf : unit -> IEnumerable<JToken>
  member Ancestors : unit -> IEnumerable<JToken>
  member AncestorsAndSelf : unit -> IEnumerable<JToken>
  member Annotation<'T> : unit -> 'T + 1 overload
  member Annotations<'T> : unit -> IEnumerable<'T> + 1 overload
  member BeforeSelf : unit -> IEnumerable<JToken>
  member Children : unit -> JEnumerable<JToken> + 1 overload
  ...

Full name: Newtonsoft.Json.Linq.JToken
Multiple items
type JProperty =
  inherit JContainer
  new : other:JProperty -> JProperty + 2 overloads
  member Name : string
  member Type : JTokenType
  member Value : JToken with get, set
  member WriteTo : writer:JsonWriter * [<ParamArray>] converters:JsonConverter[] -> unit
  static member Load : reader:JsonReader -> JProperty + 1 overload

Full name: Newtonsoft.Json.Linq.JProperty

--------------------
JProperty(other: JProperty) : unit
JProperty(name: string, [<System.ParamArray>] content: obj []) : unit
JProperty(name: string, content: obj) : unit
val prop : JProperty
val name : string
property JProperty.Name: string
val cont : JToken
Multiple items
property JProperty.Value: JToken

--------------------
(extension) System.Collections.Generic.IEnumerable.Value<'U>() : 'U
(extension) System.Collections.Generic.IEnumerable.Value<'T,'U (requires 'T :> JToken)>() : 'U
JToken.Value<'T>(key: obj) : 'T
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
Multiple items
type JArray =
  inherit JContainer
  new : unit -> JArray + 3 overloads
  member Add : item:JToken -> unit
  member Clear : unit -> unit
  member Contains : item:JToken -> bool
  member CopyTo : array:JToken[] * arrayIndex:int -> unit
  member GetEnumerator : unit -> IEnumerator<JToken>
  member IndexOf : item:JToken -> int
  member Insert : index:int * item:JToken -> unit
  member IsReadOnly : bool
  member Item : obj -> JToken with get, set
  ...

Full name: Newtonsoft.Json.Linq.JArray

--------------------
JArray() : unit
JArray(other: JArray) : unit
JArray([<System.ParamArray>] content: obj []) : unit
JArray(content: obj) : unit
val arr : JArray
val cont : seq<JToken>
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
Multiple items
type JObject =
  inherit JContainer
  new : unit -> JObject + 3 overloads
  member Add : propertyName:string * value:JToken -> unit
  member GetEnumerator : unit -> IEnumerator<KeyValuePair<string, JToken>>
  member GetValue : propertyName:string -> JToken + 1 overload
  member Item : obj -> JToken with get, set
  member Item : string -> JToken with get, set
  member Properties : unit -> IEnumerable<JProperty>
  member Property : name:string -> JProperty
  member PropertyValues : unit -> JEnumerable<JToken>
  member Remove : propertyName:string -> bool
  ...

Full name: Newtonsoft.Json.Linq.JObject

--------------------
JObject() : unit
JObject(other: JObject) : unit
JObject([<System.ParamArray>] content: obj []) : unit
JObject(content: obj) : unit
val o : JObject
JObject.Properties() : System.Collections.Generic.IEnumerable<JProperty>
Raw view Test code New version

More information

Link:http://fssnip.net/db
Posted:12 years ago
Author:Dmitry Lobanov
Tags: json