DocsPlaygroundBlogCommunityPackages
  • Playground
  • Blog
  • Community
  • Packages
  • X
  • Bluesky
  • GitHub
  • Forum
Language ManualAPISyntax LookupReact
v11v9.1 - v10.1v8.2 - v9.0v6.0 - v8.1
Overview
  • Introduction
  • Installation
  • Editor
Guides
  • Converting from JS
  • Dead Code Analysis in ReScript
  • Project Structure
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Dictionary
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
  • Promises
  • Async / Await
  • Tagged templates
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Reserved Keywords
  • Equality and Comparison
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
  • TypeScript
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Interop with JS Build Systems
  • Performance
  • Warning Numbers
Advanced Features
  • Extensible Variant
  • Scoped Polymorphic Types
  • Module Functions
  • Generalized Algebraic Data Types
Docs / Language Manual /
Edit

JSON

Parse

Bind to JavaScript's JSON.parse and type the return value as the type you're expecting:

ReScriptJS Output
// declare the shape of the json you're binding to
type data = {names: array<string>}

// bind to JS' JSON.parse
@scope("JSON") @val
external parseIntoMyData: string => data = "parse"

let result = parseIntoMyData(`{"names": ["Luke", "Christine"]}`)
let name1 = result.names[0]

Where data can be any type you assume the JSON is. As you can see, this compiles to a straightforward JSON.parse call. As with regular JS, this is convenient, but has no guarantee that e.g. the data is correctly shaped, or even syntactically valid. Slightly dangerous.

Stringify

Use JSON.stringify if your data is of type JSON.t or JSON.stringifyAny if it is not.

ReScriptJS Output
Console.log(JSON.stringifyAny(["Amy", "Joe"]))

Import a JSON file

Use the @module attribute to import JSON files directly.

ReScriptJS Output (Module)JS Output (CommonJS)
@module external studentNames: JSON.t = "./students.json"
Console.log(studentNames)

Advanced

Thanks to untagged variants, JSON can be encoded and decoded idiomatically. Check it out on the variants page.

© 2025 The ReScript Project

About
  • Community
  • ReScript Association
Find us on