CSON to JSON Converter
Convert CSON (CoffeeScript Object Notation) to valid JSON instantly. Supports indentation-based objects, unquoted keys, comments, and heredocs.
About
CSON (CoffeeScript Object Notation) removes the syntactic noise of JSON: no mandatory quotes on keys, no commas, no braces for top-level objects, and comments via #. The tradeoff is ambiguity. A misplaced indent shifts an entire subtree into the wrong parent, and most JSON validators reject CSON outright. This converter implements a recursive descent parser that resolves indentation into explicit nesting, strips comments, normalizes literals (TRUE โ true, yes โ true), and emits spec-compliant JSON per RFC 8259. It does not evaluate CoffeeScript expressions inside interpolated strings (#{expr}); those are preserved as literal text.
Incorrect manual conversion often produces invalid JSON that silently breaks CI pipelines, package configs, or API payloads. Common failure modes include trailing commas, unescaped control characters inside heredoc strings ('''), and numeric keys mistakenly left unquoted. This tool handles those edge cases. Note: the parser assumes well-formed CSON. Deeply malformed input with mixed tabs and spaces at inconsistent levels will produce a parse error rather than a guess.
Formulas
CSON parsing follows a deterministic grammar. The converter implements a line-by-line preprocessor followed by a recursive descent parser. The core logic resolves indentation into explicit structure:
Where base_indent is auto-detected from the first indented line (typically 2 or 4 spaces, or 1 tab). Each increase in indent_level opens a new object or array scope. Each decrease closes scopes until the level matches.
The parser classifies each non-empty, non-comment line into one of three categories:
Literal resolution uses pattern matching: true, yes, on โ true; false, no, off โ false; null, undefined โ null. Numeric strings are tested against the pattern regex: /^[+-]?(0x[\da-f]+|0o[0-7]+|0b[01]+|\d+\.?\d*([eE][+-]?\d+)?)$/i. Everything else is treated as an unquoted string or, if it contains a colon after the first word, a key-value pair.
Reference Data
| CSON Feature | Syntax Example | JSON Equivalent | Notes |
|---|---|---|---|
| Unquoted key | name: "Alice" | "name": "Alice" | Keys auto-quoted in output |
| Single-quoted string | "hello" | "hello" | No escape processing in CSON singles |
| Comment | # this is ignored | (removed) | Only line comments, no block comments |
| Boolean yes/no | yes | true | Also accepts on/off |
| Boolean true/false | true | true | Case-sensitive |
| Null literal | null | null | Also accepts undefined โ null |
| Integer | 42 | 42 | Preserved as number type |
| Hex number | 0xFF | 255 | Converted to decimal |
| Float | 3.14 | 3.14 | Scientific notation supported: 1e10 |
| Implicit object (indent) | person: age: 30 | {"person":{"age":30}} | Indentation determines nesting |
| Inline object | {a: 1, b: 2} | {"a":1,"b":2} | Braces make it explicit |
| Inline array | [1, 2, 3] | [1,2,3] | Standard bracket syntax |
| Heredoc string (single) | ''' multi line ''' | "multi\nline" | Preserves newlines, no escaping |
| Heredoc string (double) | """ multi line """ | "multi\nline" | Escape sequences processed |
| Multiline array (indent) | items: "a" "b" | "items":["a","b"] | Implicit array from bare values |
| Trailing comma | {a: 1,} | {"a":1} | Tolerated and stripped |
| String interpolation | "Hello #{name}" | "Hello #{name}" | Not evaluated, kept as literal |
| Infinity | Infinity | null | JSON has no Infinity; mapped to null |
| NaN | NaN | null | JSON has no NaN; mapped to null |