Relaxed JSON

Relaxed JSON (RJSON) 

RJSON (Relaxed Javascript Object Notation) allows for concise and succinct JSON authoring and parsing.

Download the official NPM Package here: https://www.npmjs.com

relax

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

Our flavor of RJSON goes beyond other variants, including JSON5. You can try the online demo here.


Table Of Contents


The Bottom Line 

Relax, it's simple!


Commas 

Commas are optional between object key-value pairs and array items.

[
    "one" 
    "two"
    "three"
]
{
    "one" : "1"
    "two" : "2"
    "three" : "3"
}
[
    {
        "one" : "1"
        "two" : "2"
    }
    {
        "three" : "3"
        "four" : "4"
    }
    "five"
    "six"
]

If you are a comma lover, trailing commas are allowed.

[ 1, 2, 3, ]
{a:1, b:2, c:3,}

Quotes 

Single, Double, or Backtick Quotes 

Both single-quote and double-quote pairs are valid in RJSON. The opposite quote character does not need to be escaped.

[
    "Hello world"
    'Hello world'
    `Hello world`
    "He said 'What'?"
    'He said "What"?'
    'A "double" and \'single\' quote'
    "A \"double\" and 'single' quote"
    `This has unescaped " and ' in the string`
]

Optional Quotes 

No quotes are needed around simple-keys 

If a key does not contain an unescaped reserved character, it is a simple-key and does not require quotes. Reserved words (numbers and the values true, false, and null are always interpreted as strings when used as an object key.

{
    1 : one
    2 : two
    true : "the key is the string true"
    escape\:me : "the key has an escaped reserved character (not recommended)"
    a\ space : "the key has an escaped space (not recommended)"
}

No quotes are needed around simple-values 

If a value does not contain an unescaped reserved character, and is not a reserved word, it is a simple-value and does not require quotes.

[ milk, eggs, fruit, 'candy bars' ]

or even better, lose the commas:

[ milk eggs fruit 'candy bars' ]

using escape (not recommended):

[ milk eggs fruit candy\ bars ]

Multline Strings 

A string can continue to a new line if the line ends with a '\'.

"A string \
on 3 \
lines"
'Another string \
on 3 \
lines'
{ aKey : "A string value \
on 3 \
lines"
}

Special Considerations 

Reserved Characters 

Keys and values containing these characters must either be:

[
    a-simple-string
    another_simple_string
    a\ string\ with\ escaped\ spaces
    a_string_with_a_\,(comma)
]

We recommend using quotes instead of escaped characters, especially for spaces.

[
    'a string with escaped spaces'
    'a_string_with_a_,(comma)'
]

Reserved Words 

These word sequences must be quoted when used as a string value:

true false null <any-parsable-number> // /*

If a simple-value can be parsed as a number, it will become a JSON number.

{ number: 1.0}

will become

{ "number" : 1 }

A simple-key will always be parsed as a string, even if it is a number or reserved word.

{ 1:1, true:true } 

will become

{ "1":1, "true":true }

Escaped Characters 

Reserved Characters can be escaped with a '\' to avoid using quotes. We do not recommend this as a general rule however.

[ it\:em item\ with\ spaces]
[ "it:em",  "item with spaces"]

Comments 

Both single-line and multi-line comments are allowed. Comments use Java/Javascript conventions. See Mozilla-Comments for the specification.

/* This is an 
RJSON file */

{ shopping-list: [ milk butter /* don't forget the beer */ beer ] } // This is a shopping list.

Edit Page On Grav


This site is StackEditPro enabled.