Reputation: 53803
Let's say I have this JSON array of objects:
[
{"name": "foo", "tags": ["bird", "animal"], "age": 10},
{"name": "bar", "tags": ["dog", "animal"], "age": 5},
{"name": "baz", "tags": ["cat", "animal"], "age": 3}
]
How can I decode this in ReasonML?
Upvotes: 1
Views: 771
Reputation: 29106
Using bs-json to decode it into an array of records:
let data = {|[
{"name": "foo", "tags": ["bird", "animal"], "age": 10},
{"name": "bar", "tags": ["dog", "animal"], "age": 5},
{"name": "baz", "tags": ["cat", "animal"], "age": 3}
]|};
type t = {
name: string,
tags: array(string),
age: int
};
module Decode = {
let item = json =>
Json.Decode.{
name: json |> field("name", string),
tags: json |> field("tags", array(string)),
age: json |> field("age", int)
};
let all =
Json.Decode.array(item)
};
let result: array(t) =
data |> Json.parseOrRaise
|> Decode.all;
Upvotes: 6