title: Decode a json tree structure
body: <p>In the <strong>bs-json</strong> library there is an example provided for converting a json structure to a tree using the <code>andThen</code> combinator. The original example can be found <a href="https://github.com/glennsl/bs-json/blob/b8218938959a2b20d8b6c8ee2600b75e5bdcb1cb/src/Json_decode.mli#L434-L472" rel="nofollow noreferrer">here on github</a> in the ML interface file. Copying the function <code>decodeTree</code> from this file to the browser reason tools shows a syntax error.</p>
  
  <p>Would very much appreciate any pointers for getting this to work.</p>
  
  <p>My attempt at translating it to Reason3 results in a type error. This is the code:</p>
  
  <pre><code>type tree('a) =
    | Node('a, list(tree('a)))
    | Leaf('a);
  
  let json = {| {
    "type": "node",
    "value": 9
    "children": [{
      "type": "leaf",
      "value": 5,
      "children": [{
        "type": "leaf",
        "value": 3
      }, {
        "type": "leaf",
        "value": 2
      }]
    }, {
        "type": "leaf",
        "value": 4
    }]
  } |};
  
  let decodeTree = (decodeValue, json) =&gt;
    Json.Decode.(
      field("type", string)
      |&gt; andThen(type_ =&gt;
           switch type_ {
           | "node" =&gt;
             Node(
               field("value", decodeValue),
               field("children", children =&gt;
                 children |&gt; array(decodeTree) |&gt; map(Array.to_list)
               )
             )
           | "leaf" =&gt; Leaf(field("value", decodeValue))
           }
         )
    );
  
  let myTree = json |&gt; Json.parseOrRaise |&gt; decodeTree(Json.Decode.int);
  </code></pre>
  
  <p>This is the type error,</p>
  
  <pre><code>This has type:
     tree('a)
  But somewhere wanted:
     Json.Decode.decoder('b) (defined as (Js.Json.t) =&gt; 'b)
  </code></pre>