---
area: "API"
parent: "Formats"
url: "https://unpoly.com/api/relaxed-json"
---
<nav aria-label="Unpoly docs">[All docs](https://unpoly.com/index.md) · [Formats](https://unpoly.com/api/formats.md)</nav>

# Relaxed JSON

_Relaxed JSON_ is a JSON superset that aims to be easier to write by humans.

<nav aria-label="Contents">

#### Contents

- [Example](#example)
- [Syntax rules](#syntax-rules)
- [What Unpoly sends](#what-unpoly-sends)
- [Parsing relaxed JSON](#parsing-relaxed-json)

</nav>

## Example {#example}

Unpoly often accepts JSON in HTML attributes or HTTP headers:

```html
<span class='user' up-data='{ "name": "Bob", "age": 18 }'>Bob</span>
```

To be easier on the hand and eyes, Unpoly also accepts single-quoted strings and unquoted property names:

```html
<span class="user" up-data="{ name: 'Bob', age: 18 }">Bob</span>
```

## Syntax rules {#syntax-rules}

- Every JSON object is also a Relaxed JSON object.
- Relaxed JSON allows single-quoted strings, in addition to double-quoted strings.
- Relaxed JSON allows unquoted property names, like JavaScript. It's also OK to double-quote property names.
- Relaxed JSON allows [trailing commas](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas) in object literals and array literals.

## What Unpoly sends {#what-unpoly-sends}

When Unpoly sends JSON (e.g. in the [`X-Up-Context`](https://unpoly.com/api/X-Up-Context.md) header), it always produces regular JSON.

If you implement the [optional server protocol](https://unpoly.com/api/up.protocol.md) you only need to deal with regular JSON strings.

## Parsing relaxed JSON {#parsing-relaxed-json}

Your frontend code can parse relaxed JSON using [`up.util.parseRelaxedJSON()`](https://unpoly.com/api/up.util.parseRelaxedJSON.md):

```js
let value = up.util.parseRelaxedJSON("{ foo: 'one', bar: 'two', }")
console.log(value) // logs an object { foo: 'one', bar: 'two' }
```

On the backend we recommend using a [JSON5](https://json5.org/) parser, which is a superset of relaxed JSON. JSON5 parsers are available in [many languages](https://github.com/json5/json5/wiki/In-the-Wild).

