Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including bs-decode with all npm packages installed. Try it out:

bs-decode lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually:

require("bs-abstract/package.json"); // bs-abstract is a peer dependency. require("relude/package.json"); // relude is a peer dependency. // require("bs-decode/[??]")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

bs-decode v0.9.0

Type-safe JSON decoding for ReasonML and OCaml


build status test coverage npm version license

Read the Documentation

Decode JSON values into structured ReasonML and OCaml types. Inspired by Elm's Json.Decode and the Decode Pipeline, bs-decode is an alternative to bs-json that focuses on structured, type-safe error handling, rather than exceptions. Additionally, bs-decode collects up everything that went wrong while parsing the JSON, rather than failing on the first error.


Install via npm:

npm install --save bs-decode

Update your bsconfig.json

"bs-dependencies": [


The following is available to give you an idea of how the library works, but the complete documentation will probably be more useful if you want to write your own decoders.

// imagine you have a `user` type and `make` function to construct one
type user = {
  name: string,
  age: int,
  isAdmin: bool,
  lastLogin: option(Js.Date.t)

let make = (name, age, isAdmin, lastLogin) =>
  { name, age, isAdmin, lastLogin };

 * Given a JSON value that looks like:
 * { "name": "Michael", "age": 32, "roles": ["admin"] }
 * you can write a function to convert this JSON into a value of type `user`
module Decode = Decode.AsResult.OfParseError; // module alias for brevity

let decode = json =>
    |> field("name", string)
    |> field("age", intFromNumber)
    |> field("roles", map(List.contains("admin"), list(string)))
    |> optionalField("lastLogin", date)
    |> run(json)

let myUser = decode(json); /* Ok({ name: "Michael", ...}) */


All contributions are welcome! This obviously includes code changes and documentation improvements (see CONTRIBUTING), but we also appreciate any feedback you want to provide (in the form of Github issues) about concepts that are confusing or poorly explained in the docs.


Released under the MIT license.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free