module Util:sig..end
Here is some sample JSON data:
{
"id": "398eb027",
"name": "John Doe",
"pages": [
{
"id": 1,
"title": "The Art of Flipping Coins",
"url": "http://example.com/398eb027/1"
},
{
"id": 2,
"deleted": true
},
{
"id": 3,
"title": "Artichoke Salad",
"url": "http://example.com/398eb027/3"
},
{
"id": 4,
"title": "Flying Bananas",
"url": "http://example.com/398eb027/4"
}
]
}
In order to extract the "id" field, assuming it is mandatory, we would use the following OCaml code that operates on single JSON nodes:
open Yojson.Basic.Util ... let id = json |> member "id" |> to_string in ...
In order to extract all the "title" fields, we would write the following OCaml code that operates on lists of JSON nodes, skipping undefined nodes and nodes of unexpected type:
open Yojson.Basic.Util
let extract_titles (json : Yojson.Basic.json) : string list =
[json]
|> filter_member "pages"
|> flatten
|> filter_member "title"
|> filter_stringexception Type_error of string * Yojson.Basic.json
member on an `Int. The string message explains the
mismatch.exception Undefined of string * Yojson.Basic.json
val (|>) : 'a -> ('a -> 'b) -> 'bval member : string -> Yojson.Basic.json -> Yojson.Basic.jsonmember k obj returns the value associated with the key k in the JSON
object obj, or `Null if k is not present in obj.val index : int -> Yojson.Basic.json -> Yojson.Basic.jsonindex i arr returns the value at index i in the JSON array arr.
Negative indices count from the end of the list (so -1 is the last
element).val map : (Yojson.Basic.json -> Yojson.Basic.json) ->
Yojson.Basic.json -> Yojson.Basic.jsonmap f arr calls the function f on each element of the JSON array
arr, and returns a JSON array containing the results.val to_assoc : Yojson.Basic.json -> (string * Yojson.Basic.json) listType_error.val to_option : (Yojson.Basic.json -> 'a) -> Yojson.Basic.json -> 'a optionNone if the JSON value is null or map the JSON value
to Some value using the provided function.val to_bool : Yojson.Basic.json -> boolType_error.val to_bool_option : Yojson.Basic.json -> bool optionSome boolean value,
return None if the value is null,
or raise Type_error otherwise.val to_number : Yojson.Basic.json -> floatType_error.val to_number_option : Yojson.Basic.json -> float optionSome number,
return None if the value is null,
or raise Type_error otherwise.val to_float : Yojson.Basic.json -> floatType_error.
to_number is generally preferred as it also works with int literals.val to_float_option : Yojson.Basic.json -> float optionSome float value,
return None if the value is null,
or raise Type_error otherwise.
to_number_option is generally preferred as it also works
with int literals.val to_int : Yojson.Basic.json -> intType_error.val to_int_option : Yojson.Basic.json -> int optionSome int from a JSON int,
return None if the value is null,
or raise Type_error otherwise.val to_list : Yojson.Basic.json -> Yojson.Basic.json listType_error.val to_string : Yojson.Basic.json -> stringType_error.val to_string_option : Yojson.Basic.json -> string optionSome string from a JSON string,
return None if the value is null,
or raise Type_error otherwise.val convert_each : (Yojson.Basic.json -> 'a) -> Yojson.Basic.json -> 'a listmap, because they do
not return JSON values. This convenience function convert_each to_f arr
is equivalent to List.map to_f (to_list arr).val filter_map : ('a -> 'b option) -> 'a list -> 'b listfilter_map f l maps each element of the list l to an optional value
using function f and unwraps the resulting values.val flatten : Yojson.Basic.json list -> Yojson.Basic.json listflatten l is equivalent to List.flatten (filter_list l).val filter_index : int -> Yojson.Basic.json list -> Yojson.Basic.json listval filter_list : Yojson.Basic.json list -> Yojson.Basic.json list listval filter_member : string -> Yojson.Basic.json list -> Yojson.Basic.json listval filter_assoc : Yojson.Basic.json list -> (string * Yojson.Basic.json) list listval filter_bool : Yojson.Basic.json list -> bool listval filter_int : Yojson.Basic.json list -> int list`Int nodes) and unwraps them.val filter_float : Yojson.Basic.json list -> float list`Float nodes) and unwraps them.val filter_number : Yojson.Basic.json list -> float list`Int or `Float) and unwraps them.
Ints are converted to floats.val filter_string : Yojson.Basic.json list -> string list