Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Community
  • X
  • Bluesky
  • GitHub
  • Forum
JS Module
Overview
  • JS
Submodules
  • Array2
  • Array
  • Console
  • Date
  • Dict
  • Exn
  • Float
  • Global
  • Int
  • Json
    • t
    • array
    • boolean
    • booleanArray
    • classify
    • decodeArray
    • decodeBoolean
    • decodeNull
    • decodeNumber
    • decodeObject
    • decodeString
    • kind
    • null
    • number
    • numberArray
    • object_
    • objectArray
    • parseExn
    • string
    • stringArray
    • stringify
    • stringifyAny
    • stringifyWithSpace
    • tagged_t
    • test
  • List
  • Math
  • NullUndefined
  • Null
  • Nullable
  • Obj
  • Option
  • Promise
  • Re
  • Result
  • String2
  • String
  • TypedArrayArrayBuffer
  • TypedArrayDataView
  • TypedArrayFloat32Array
  • TypedArrayFloat64Array
  • TypedArrayInt8Array
  • TypedArrayInt16Array
  • TypedArrayInt32Array
  • TypedArrayTypeS
  • TypedArrayUint8Array
  • TypedArrayUint8ClampedArray
  • TypedArrayUint16Array
  • TypedArrayUint32Array
  • TypedArray2ArrayBuffer
  • TypedArray2DataView
  • TypedArray2Float32Array
  • TypedArray2Float64Array
  • TypedArray2Int8Array
  • TypedArray2Int16Array
  • TypedArray2Int32Array
  • TypedArray2Uint8Array
  • TypedArray2Uint8ClampedArray
  • TypedArray2Uint16Array
  • TypedArray2Uint32Array
  • TypedArray2
  • TypedArray
  • Types
  • Undefined
  • Vector
API / Js / Json

Json

Provide utilities for JSON.

t

RES
type t

The JSON data structure.

kind

RES
type rec kind<'a> = | String: kind<Js_string.t> | Number: kind<float> | Object: kind<Js_dict.t<t>> | Array: kind<array<t>> | Boolean: kind<bool> | Null: kind<Js_types.null_val>

Underlying type of a JSON value.

tagged_t

RES
type tagged_t = | JSONFalse | JSONTrue | JSONNull | JSONString(string) | JSONNumber(float) | JSONObject(Js_dict.t<t>) | JSONArray(array<t>)

classify

RES
let classify: t => tagged_t

test

RES
let test: ('a, kind<'b>) => bool

test(v, kind) returns true if v is of kind.

decodeString

RES
let decodeString: t => option<Js_string.t>

decodeString(json) returns Some(s) if json is a string, None otherwise.

decodeNumber

RES
let decodeNumber: t => option<float>

decodeNumber(json) returns Some(n) if json is a number, None otherwise.

decodeObject

RES
let decodeObject: t => option<Js_dict.t<t>>

decodeObject(json) returns Some(o) if json is an object, None otherwise.

decodeArray

RES
let decodeArray: t => option<array<t>>

decodeArray(json) returns Some(a) if json is an array, None otherwise.

decodeBoolean

RES
let decodeBoolean: t => option<bool>

decodeBoolean(json) returns Some(b) if json is a boolean, None otherwise.

decodeNull

RES
let decodeNull: t => option<Js_null.t<'a>>

decodeNull(json) returns Some(null) if json is a null, None otherwise.

null

RES
let null: t

null is the singleton null JSON value.

string

RES
let string: string => t

string(s) makes a JSON string of the string s.

number

RES
let number: float => t

number(n) makes a JSON number of the float n.

boolean

RES
let boolean: bool => t

boolean(b) makes a JSON boolean of the bool b.

object_

RES
let object_: Js_dict.t<t> => t

object_(dict) makes a JSON object of the Js.Dict.t dict.

array

RES
let array: array<t> => t

array_(a) makes a JSON array of the Js.Json.t array a.

stringArray

RES
let stringArray: array<string> => t

stringArray(a) makes a JSON array of the string array a.

numberArray

RES
let numberArray: array<float> => t

numberArray(a) makes a JSON array of the float array a.

booleanArray

RES
let booleanArray: array<bool> => t

booleanArray(a) makes a JSON array of the bool array a.

objectArray

RES
let objectArray: array<Js_dict.t<t>> => t

objectArray(a) makes a JSON array of the JsDict.tarraya`.

parseExn

RES
let parseExn: string => t

parseExn(s) parses the string s into a JSON data structure. Returns a JSON data structure. Raises SyntaxError if the given string is not a valid JSON. Note: SyntaxError is a JavaScript exception.

RES
/* parse a simple JSON string */ let json = try Js.Json.parseExn(` "hello" `) catch { | _ => failwith("Error parsing JSON string") } switch Js.Json.classify(json) { | Js.Json.JSONString(value) => Js.log(value) | _ => failwith("Expected a string") }
RES
/* parse a complex JSON string */ let getIds = s => { let json = try Js.Json.parseExn(s) catch { | _ => failwith("Error parsing JSON string") } switch Js.Json.classify(json) { | Js.Json.JSONObject(value) => /* In this branch, compiler infer value : Js.Json.t Js.Dict.t */ switch Js.Dict.get(value, "ids") { | Some(ids) => switch Js.Json.classify(ids) { | Js.Json.JSONArray(ids) => /* In this branch compiler infer ids : Js.Json.t array */ ids | _ => failwith("Expected an array") } | None => failwith("Expected an `ids` property") } | _ => failwith("Expected an object") } } /* prints `1, 2, 3` */ Js.log(getIds(` { "ids" : [1, 2, 3 ] } `))

stringify

RES
let stringify: t => string

stringify(json) formats the JSON data structure as a string. Returns the string representation of a given JSON data structure.

RES
/* Creates and stringifies a simple JS object */ { let dict = Js.Dict.empty() Js.Dict.set(dict, "name", Js.Json.string("John Doe")) Js.Dict.set(dict, "age", Js.Json.number(30.0)) Js.Dict.set(dict, "likes", Js.Json.stringArray(["bucklescript", "ocaml", "js"])) Js.log(Js.Json.stringify(Js.Json.object_(dict))) }

stringifyWithSpace

RES
let stringifyWithSpace: (t, int) => string

stringify(json) formats the JSON data structure as a string. Returns the string representation of a given JSON data structure with spacing.

RES
/* Creates and stringifies a simple JS object with spacing */ { let dict = Js.Dict.empty() Js.Dict.set(dict, "name", Js.Json.string("John Doe")) Js.Dict.set(dict, "age", Js.Json.number(30.0)) Js.Dict.set(dict, "likes", Js.Json.stringArray(["bucklescript", "ocaml", "js"])) Js.log(Js.Json.stringifyWithSpace(Js.Json.object_(dict), 2)) }

stringifyAny

RES
let stringifyAny: 'a => option<string>

stringifyAny(value) formats any value into a JSON string.

RES
/* prints `["hello", "world"]` */ Js.log(Js.Json.stringifyAny(["hello", "world"]))

© 2025 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on