---
title: really-relaxed-json
url: 'https://code-evolve.com/resources/really-relaxed-json'
markdown: 'https://code-evolve.com/resources/really-relaxed-json.md'
date: '2026-10-15'
description: "Relaxed JSON (RJSON) See http://www.relaxedjson.org/ for specification and details. This library is used by the Online RJSON Parser and Formatter. Quick Usage Simple conversion from RJSON to compact JSON. import {toJson} from 'really-relaxed-json' const rjson = '[ one two three {foo:bar} ]' const j…"
---

# Relaxed JSON (RJSON)

See <http://www.relaxedjson.org/> for specification and details.

This library is used by the [Online RJSON Parser and Formatter](http://www.relaxedjson.org/docs/converter.html).

# Quick Usage

Simple conversion from RJSON to compact JSON.

```javascript
import {toJson} from 'really-relaxed-json'
const rjson = '[ one two three {foo:bar} ]'
const json = toJson(rjson)
// json value is ["one", "two", "three", {"foo": "bar"}]
```

You can also go the other way, from JSON to RJSON.

```javascript
import {toRJson} from 'really-relaxed-json'
const json = '["one", "two", "three", {"foo": "bar"}]'
const rjson = toRJson(json)
// rjson value is [ one two three {foo:bar} ]
```

You can even convert from JSON or RJSON to Javascript.

```javascript
import {toJs} from 'really-relaxed-json'
const js = toJs('["one", "two", "three", {"foo": "bar"}]')
// js value is ['one', 'two', 'three', {foo: "bar"}]
```

Simple helper functions are exported by the module.

```javascript
import {toJson, toRJson, toJs, prettyPrint, Options} from 'really-relaxed-json'
```

You can also use the full-blown API for more control.

```javascript
import {api} from 'really-relaxed-json'
```

# Overview

In the spirit of the original JSON, there are only a few simple rules.

- All standard JSON is valid. See [json.org](https://www.json.org/) for the specification.
- Commas are optional between objects pairs and array items.
- Trailing commas are allowed.
- Quotes are optional around *simple-keys* and *simple-values*. We call these *Bare Strings*.
- Single-quote, double-quote, and backtick quote pairs can be used for strings.
- Multiline strings are allowed.
- Single-line and multi-line comments are allowed.

You can write RJSON like this:
View this example on the [Online RJSON Parser and Formatter](http://www.relaxedjson.org/docs/converter.html?source=%7B%0D%0A%09%2F%2F%20This%20is%20a%20sample%20RJSON%20file%0D%0A%09%0D%0A%09buy%3A%20%5Bmilk%20eggs%20butter%20%27dog%20bones%27%5D%0D%0A%09tasks%3A%20%5B%20%7Bname%3Aexercise%20completed%3Afalse%7D%20%7Bname%3Aeat%20completed%3Atrue%7D%20%5D%0D%0A%09%0D%0A%09%27another%20key%27%20%3A%20%27another%20value%27%0D%0A%09%0D%0A%09%2F%2A%20%20It%20is%20very%20easy%20%0D%0A%09%09to%20read%20and%20write%20RJSON%0D%0A%09%09without%20quotes%20or%20commas%21%0D%0A%09%2A%2F%0D%0A%7D)

```rjson
{  
 // This is a sample RJSON file  
 buy: [milk eggs butter 'dog bones']  
 tasks: [ {name:exercise completed:false} {name:eat completed:true} ]  
 'another key' : 'another value'  
  /*  It is very easy   
  to read and write RJSON  
 without quotes or commas! */
}  
```

Convert it to JSON like this:

Simple conversion from RJSON to compact JSON:

```javascript
import {toJson} from 'really-relaxed-json'
const jsonString = toJson('[ one two three {foo:bar} ]');  
```

Using a parser object:

```javascript
var parser = require('really-relaxed-json').createParser();  
var json = parser.stringToValue('[ one two three {foo:bar} ]');  
console.log(json.toString());  
// or  
var jsonString = parser.stringToJson('[ one two three {foo:bar} ]');  
console.log(jsonString);  
```

Use this library as:

- A pre-processor for ease-of-authoring
- An input/output protocol
- Converting to and from JSON, RJSON, and Javascript
- Pretty printing JSON, RJSON, and Javascript
- Working with a JSON value model

# Installation

```sh
npm install --save really-relaxed-json  
```

# Node API

The following convenience functions are exported by the Node module

- toJson(rjson:String, compact:Boolean=true) : String
- toRJson(json:String, compact:Boolean=true) : String
- toJs(rjson:String, compact:Boolean=true) : String
- prettyPrint(options:Options, stringOrValue:\[String|JsonValue\]) : String
- createParser : function
- Options : PrettyPrinter.Options
- api : Object (The full-blown API)

The API also contains classes for formatting and manipulating JSON, RJSON, and Javascript.

# Parser Usage

[Parser API](http://www.relaxedjson.org/docs/api/tv.twelvetone.rjson/-r-json-parser/index.html)

```javascript
import {toJson} from 'really-relaxed-json'
var json = toJson("[one two three {foo:bar} ] /* a comment */");  
console.log(json);  
```

```javascript
var parser = require('really-relaxed-json').createParser();
var jsonValue = parser.stringToValue("[one two three {foo:bar} ] /* a comment */");  
// Use toString to print compact JSON, or PrettyPrint it.
console.log(jsonValue.toString());  

// Or simply convert directly to a Json string.
// You can feed in either JSON or RJSON with the output always being JSON.
var jsonString = parser.stringToJson("[one two three {foo:bar} ] /* a comment */");  
console.log(jsonString);  
```

# Formatter/Converter Usage

[Pretty Printer API](http://www.relaxedjson.org/docs/api/tv.twelvetone.rjson/-pretty-printer/index.html)

```javascript
import {prettyPrinter, Options} from 'really-relaxed-json'
var prettyString = prettyPrint(Options.JsonPretty, '[a b c]');  
```

## Using The Options Object

Use one of the presets.

```javascript
 import {Options} from 'really-relaxed-json'
 var opts;  
  // For JSON
  opts = Options.JsonCompact;  
  opts = Options.JsonPretty;  
  // For RJSON
  opts = Options.RJsonCompact;  
  opts = Options.RJsonPretty;  
  // For Javascript Object
  opts = Options.JsoCompact;  
  opts = Options.JsoPretty;  
```

Or customize the options.

```javascript
 var opts = new Options();  
  opts.useQuotes = true;  
  opts.useArrayCommas = true;  
  opts.useObjectCommas = true;  
  opts.objectItemNewline = true;  
  opts.arrayItemNewline = true;  
  opts.isSpaceAfterComma = true;  
  opts.isSpaceAfterColon = true;
```

# API

## NPM

The NPM API has a convenience function `toJson`. It converts RJSON, JSON, or Javascript to a compact or pretty JSON string, perfect
for pipelining.

```kotlin
fun toJson(rjsonString:String, compact:Boolean = true) : String  
```

# Official API Documentation

The Java and Javascript API are the same. <http://www.relaxedjson.org/docs/api/>

## Parser API

[Parse API](http://www.relaxedjson.org/docs/api/tv.twelvetone.rjson/-r-json-parser/index.html)

## PrettyPrinter API

[Pretty Printer API](http://www.relaxedjson.org/docs/api/tv.twelvetone.rjson/-pretty-printer/index.html)

## JSON Value Model

[JSON Value API](http://www.relaxedjson.org/docs/api/tv.twelvetone.json/index.html)

# Performance

Version 0.4.0 converts **5.0–11.6× faster than 0.3.3** and **3.4–7.4× faster than json5**. Against hjson it is **1.9× faster on large strict JSON** and on par on relaxed input, where the two were within 2% of each other, less than the variation between rounds.

Conversions per second, converting text to a JSON string (higher is better):

| Document | 0.4.0 | 0.3.3 | json5 2.2.3 | hjson 3.2.2 | `JSON.parse` |
|---|---|---|---|---|---|
| Large strict JSON, 433.5 KB | 461.3 | 39.8 | 62.3 | 246.9 | 1119.2 |
| Relaxed, 1000 records, 156.7 KB | 469.7 | 78.5 | 123.3 | 478.0 | n/a |
| Relaxed, small config, 0.3 KB | 186,592 | 37,465 | 55,502 | 188,015 | n/a |

How it was measured, so it can be repeated:

- The task is the same for every library: `toJson(text)` here, `JSON.stringify(parse(text))` for the others. Each library's result is checked against the reference parse before it is timed.
- The relaxed documents use only syntax all three relaxed parsers accept: line and block comments, unquoted keys, single-quoted strings with escaped quotes, and trailing commas. `JSON.parse` cannot read them.
- Seven interleaved rounds of one second per library and document, reporting the median. Rounds varied by 11% or less.
- Node 23.10.0 on an Apple M5 Max, October 2026.

# Important Changes

Starting with version 0.2.0, we have removed the dependency to Kotlin by embedding a tree-shaken version. As of 0.4.0 the combined module, including the parser, formatter, utilities, and runtime, is 230 KB (53 KB gzipped).

Starting with version 0.2.2, we have added backtick quote support.

Starting with version 0.2.7, you can now convert to and from JavaScript, Json, and RJson.

Starting with version 0.2.9, we have completed replaced our internal parser with one that handles very large files.

Starting with version 0.2.17, we have changed the way the '\\' character escapes bare strings.

Starting with version 0.2.21, we have added an option to encode unicode characters when PrettyPrinting.

Starting with version 0.2.22, we have added aliases to access Options (bypassing the Companion object).

Starting with version 0.3.0, we have upgraded to use the latest Kotlin multiplatform conventions. When using in a browser environment, the API is an alias for an internal module now named `window.really_relaxed_json`.

Starting with version 0.4.0, parsed strings hold their decoded text: reading `"x\ny"` gives a newline rather than a backslash and `n`. Numbers that are valid JSON are printed exactly as written. See the CHANGELOG for the full list of fixes.

---

## Navigation

- Previous: [snackbarstack](https://code-evolve.com/resources/snackbarstack.md)
