API / Core / Json

JSON

t

RESCRIPT
type t = Js.Json.t

A type representing a JSON object.

parseExn

RESCRIPT
let parseExn: string => t

parseExn(string)

Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid. It returns a JSON type.

Examples

RESCRIPT
try { let _ = JSON.parseExn(`{"foo":"bar","hello":"world"}`) // { foo: 'bar', hello: 'world' } let _ = JSON.parseExn("") // error } catch { | Exn.Error(obj) => Console.log("error") }

Exceptions

  • Raises a SyntaxError (Exn.t) if the string isn't valid JSON.

parseExnWithReviver

RESCRIPT
let parseExnWithReviver: (string, (string, t) => t) => t

parseExnWithReviver(string, reviver)

Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid. The reviver describes how the value should be transformed. It is a function which receives a key and a value. It returns a JSON type.

Examples

RESCRIPT
let reviver = (key, value) => { let valueType = JSON.Classify.classify(value) switch valueType { | String(string) => string->String.toUpperCase->JSON.Encode.string | Number(number) => (number *. 2.0)->JSON.Encode.float | _ => value } } let jsonString = `{"hello":"world","someNumber":21}` try { JSON.parseExnWithReviver(jsonString, reviver)->Console.log // { hello: 'WORLD', someNumber: 42 } JSON.parseExnWithReviver("", reviver)->Console.log // error } catch { | Exn.Error(_) => Console.log("error") }

Exceptions

  • Raises a SyntaxError if the string isn't valid JSON.

stringify

RESCRIPT
let stringify: t => string

stringify(json)

Converts a JSON object to a JSON string. If you want to stringify any type, use JSON.stringifyAny instead.

Examples

RESCRIPT
let json = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ])->JSON.Encode.object JSON.stringify(json) // {"foo":"bar","hello":"world","someNumber":42}

stringifyWithIndent

RESCRIPT
let stringifyWithIndent: (t, int) => string

stringifyWithIndent(json, indentation)

Converts a JSON object to a JSON string. The output will be indented. If you want to stringify any type, use JSON.stringifyAnyWithIndent instead.

Examples

RESCRIPT
let json = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ])->JSON.Encode.object JSON.stringifyWithIndent(json, 2) // { // "foo": "bar", // "hello": "world", // "someNumber": 42 // }

stringifyWithReplacer

RESCRIPT
let stringifyWithReplacer: (t, (string, t) => t) => string

stringifyWithReplacer(json, replacer)

Converts a JSON object to a JSON string. The replacer describes how the value should be transformed. It is a function which receives a key and a value. If you want to stringify any type, use JSON.stringifyAnyWithReplacer instead.

Examples

RESCRIPT
let json = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ])->JSON.Encode.object let replacer = (key, value) => { let decodedValue = value->JSON.Decode.string switch decodedValue { | Some(string) => string->String.toUpperCase->JSON.Encode.string | None => value } } JSON.stringifyWithReplacer(json, replacer) // {"foo":"BAR","hello":"WORLD","someNumber":42}

stringifyWithReplacerAndIndent

RESCRIPT
let stringifyWithReplacerAndIndent: (t, (string, t) => t, int) => string

stringifyWithReplacerAndIndent(json, replacer, indentation)

Converts a JSON object to a JSON string. The output will be indented. The replacer describes how the value should be transformed. It is a function which receives a key and a value. If you want to stringify any type, use JSON.stringifyAnyWithReplacerAndIndent instead.

Examples

RESCRIPT
let json = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ])->JSON.Encode.object let replacer = (key, value) => { let decodedValue = value->JSON.Decode.string switch decodedValue { | Some(string) => string->String.toUpperCase->JSON.Encode.string | None => value } } JSON.stringifyWithReplacerAndIndent(json, replacer, 2) // { "foo": "BAR", "hello": "WORLD", "someNumber": 42 }

stringifyWithFilter

RESCRIPT
let stringifyWithFilter: (t, array<string>) => string

stringifyWithFilter(json, filter)

Converts a JSON object to a JSON string. The filter is an array of keys, which should be included in the output. If you want to stringify any type, use JSON.stringifyAnyWithFilter instead.

Examples

RESCRIPT
let json = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ])->JSON.Encode.object JSON.stringifyWithFilter(json, ["foo", "someNumber"]) // {"foo":"bar","someNumber":42}

stringifyWithFilterAndIndent

RESCRIPT
let stringifyWithFilterAndIndent: (t, array<string>, int) => string

stringifyWithFilterAndIndent(json, filter, indentation)

Converts a JSON object to a JSON string. The output will be indented. The filter is an array of keys, which should be included in the output. If you want to stringify any type, use JSON.stringifyAnyWithFilterAndIndent instead.

Examples

RESCRIPT
let json = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ])->JSON.Encode.object JSON.stringifyWithFilterAndIndent(json, ["foo", "someNumber"], 2) // { // "foo": "bar", // "someNumber": 42 // }

stringifyAny

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

stringifyAny(any)

Converts any type to a JSON string. Stringifying a function or undefined will return None. If the value contains circular references or BigInts, the function will throw a JavaScript exception (TypeError). If you want to stringify a JSON object, use JSON.stringify instead.

Examples

RESCRIPT
let dict = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ]) JSON.stringifyAny(dict) // {"foo":"bar","hello":"world","someNumber":42} JSON.stringifyAny(() => "hello world") // None BigInt.fromInt(0)->JSON.stringifyAny // exception

Exceptions

  • Raises a TypeError if the value contains circular references.

  • Raises a TypeError if the value contains BigInts.

stringifyAnyWithIndent

RESCRIPT
let stringifyAnyWithIndent: ('a, int) => option<string>

stringifyAnyWithIndent(any, indentation)

Converts any type to a JSON string. The output will be indented. Stringifying a function or undefined will return None. If the value contains circular references or BigInts, the function will throw a JavaScript exception (TypeError). If you want to stringify a JSON object, use JSON.stringifyWithIndent instead.

Examples

RESCRIPT
let dict = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ]) JSON.stringifyAnyWithIndent(dict, 2) // { // "foo": "bar", // "hello": "world", // "someNumber": 42 // } JSON.stringifyAny(() => "hello world") // None BigInt.fromInt(0)->JSON.stringifyAny // exception

Exceptions

  • Raises a TypeError if the value contains circular references.

  • Raises a TypeError if the value contains BigInts.

stringifyAnyWithReplacer

RESCRIPT
let stringifyAnyWithReplacer: ('a, (string, t) => t) => option<string>

stringifyAnyWithReplacer(json, replacer)

Converts any type to a JSON string. The replacer describes how the value should be transformed. It is a function which receives a key and a value. Stringifying a function or undefined will return None. If the value contains circular references or BigInts, the function will throw a JavaScript exception (TypeError). If you want to stringify a JSON object, use JSON.stringifyWithReplacer instead.

Examples

RESCRIPT
let dict = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ]) let replacer = (key, value) => { let decodedValue = value->JSON.Decode.string switch decodedValue { | Some(string) => string->String.toUpperCase->JSON.Encode.string | None => value } } JSON.stringifyAnyWithReplacer(dict, replacer) // {"foo":"BAR","hello":"WORLD","someNumber":42} JSON.stringifyAny(() => "hello world") // None BigInt.fromInt(0)->JSON.stringifyAny // exception

Exceptions

  • Raises a TypeError if the value contains circular references.

  • Raises a TypeError if the value contains BigInts.

stringifyAnyWithReplacerAndIndent

RESCRIPT
let stringifyAnyWithReplacerAndIndent: ('a, (string, t) => t, int) => option<string>

stringifyAnyWithReplacerAndIndent(json, replacer, indentation)

Converts any type to a JSON string. The output will be indented. The replacer describes how the value should be transformed. It is a function which receives a key and a value. Stringifying a function or undefined will return None. If the value contains circular references or BigInts, the function will throw a JavaScript exception (TypeError). If you want to stringify a JSON object, use JSON.stringifyWithReplacerAndIndent instead.

Examples

RESCRIPT
let dict = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ]) let replacer = (key, value) => { let decodedValue = value->JSON.Decode.string switch decodedValue { | Some(string) => string->String.toUpperCase->JSON.Encode.string | None => value } } JSON.stringifyAnyWithReplacerAndIndent(dict, replacer, 2) // { // "foo": "BAR", // "hello": "WORLD", // "someNumber": 42 // } JSON.stringifyAny(() => "hello world") // None BigInt.fromInt(0)->JSON.stringifyAny // exception

Exceptions

  • Raises a TypeError if the value contains circular references.

  • Raises a TypeError if the value contains BigInts.

stringifyAnyWithFilter

RESCRIPT
let stringifyAnyWithFilter: ('a, array<string>) => string

stringifyAnyWithFilter(json, filter)

Converts any type to a JSON string. The filter is an array of keys, which should be included in the output. Stringifying a function or undefined will return None. If the value contains circular references or BigInts, the function will throw a JavaScript exception (TypeError). If you want to stringify a JSON object, use JSON.stringifyWithFilter instead.

Examples

RESCRIPT
let dict = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ]) JSON.stringifyAnyWithFilter(dict, ["foo", "someNumber"]) // {"foo": "bar","someNumber": 42} JSON.stringifyAny(() => "hello world") // None BigInt.fromInt(0)->JSON.stringifyAny // exception

Exceptions

  • Raises a TypeError if the value contains circular references.

  • Raises a TypeError if the value contains BigInts.

stringifyAnyWithFilterAndIndent

RESCRIPT
let stringifyAnyWithFilterAndIndent: ('a, array<string>, int) => string

stringifyAnyWithFilterAndIndent(json, filter, indentation)

Converts any type to a JSON string. The output will be indented. The filter is an array of keys, which should be included in the output. Stringifying a function or undefined will return None. If the value contains circular references or BigInts, the function will throw a JavaScript exception (TypeError). If you want to stringify a JSON object, use JSON.stringifyWithFilterAndIndent instead.

Examples

RESCRIPT
let dict = Dict.fromArray([ ("foo", JSON.Encode.string("bar")), ("hello", JSON.Encode.string("world")), ("someNumber", JSON.Encode.int(42)), ]) JSON.stringifyAnyWithFilterAndIndent(dict, ["foo", "someNumber"], 2) // { // "foo": "bar", // "someNumber": 42 // } JSON.stringifyAny(() => "hello world") // None BigInt.fromInt(0)->JSON.stringifyAny // exception

Exceptions

  • Raises a TypeError if the value contains circular references.

  • Raises a TypeError if the value contains BigInts.