Back to Biomejs

noEmptyObjectKeys

src/content/docs/linter/rules/no-empty-object-keys.mdx

latest9.9 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JSON (and super languages)" icon="seti:json"> :::note This rule has been implemented but not released yet. It will be available in the next release. ::: :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Diagnostic Category: [`lint/nursery/noEmptyObjectKeys`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`json/no-empty-keys`](https://github.com/eslint/json/blob/main/docs/rules/no-empty-keys.md)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noEmptyObjectKeys": "error"
			}
		}
	}
}

Description

Disallow empty keys in JSON objects.

In JSON, using empty keys (keys that are empty strings or contain only whitespace) can lead to accessibility and maintenance issues. While technically valid in JSON, empty keys make objects harder to read, can cause confusion when debugging, and may create problems with some JSON parsers or processors. Additionally, empty keys often indicate mistakes or oversights in the processes.

Examples

Invalid

json
{
  "": "value"
}
<pre class="language-text"><code class="language-text">code-block.json:2:3 <a href="https://biomejs.dev/linter/rules/no-empty-object-keys">lint/nursery/noEmptyObjectKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unexpected empty object key.</span> <strong>1 │ </strong>&#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> &quot;&quot;: &quot;value&quot; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty keys often cause confusion and may cause issues with parsers or processors. Either remove this property or provide a meaningful key name.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
json
{
  "validKey": "value",
  "": "another value"
}
<pre class="language-text"><code class="language-text">code-block.json:3:3 <a href="https://biomejs.dev/linter/rules/no-empty-object-keys">lint/nursery/noEmptyObjectKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unexpected empty object key.</span> <strong>1 │ </strong>&#123; <strong>2 │ </strong> &quot;validKey&quot;: &quot;value&quot;, <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> &quot;&quot;: &quot;another value&quot; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>4 │ </strong>&#125; <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty keys often cause confusion and may cause issues with parsers or processors. Either remove this property or provide a meaningful key name.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
json
{
  " ": "space as key"
}
<pre class="language-text"><code class="language-text">code-block.json:2:3 <a href="https://biomejs.dev/linter/rules/no-empty-object-keys">lint/nursery/noEmptyObjectKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unexpected empty object key.</span> <strong>1 │ </strong>&#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> &quot; &quot;: &quot;space as key&quot; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty keys often cause confusion and may cause issues with parsers or processors. Either remove this property or provide a meaningful key name.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
json
{
  "\t": "tab as key"
}
<pre class="language-text"><code class="language-text">code-block.json:2:3 <a href="https://biomejs.dev/linter/rules/no-empty-object-keys">lint/nursery/noEmptyObjectKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unexpected empty object key.</span> <strong>1 │ </strong>&#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> &quot;&#92;t&quot;: &quot;tab as key&quot; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty keys often cause confusion and may cause issues with parsers or processors. Either remove this property or provide a meaningful key name.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
json
{
  "\n": "newline as key"
}
<pre class="language-text"><code class="language-text">code-block.json:2:3 <a href="https://biomejs.dev/linter/rules/no-empty-object-keys">lint/nursery/noEmptyObjectKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unexpected empty object key.</span> <strong>1 │ </strong>&#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> &quot;&#92;n&quot;: &quot;newline as key&quot; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Empty keys often cause confusion and may cause issues with parsers or processors. Either remove this property or provide a meaningful key name.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

json
{
  "key": "value"
}
</TabItem> </Tabs>