json/top-level-interop
Require the JSON top-level value to be an array or object
The first JSON specification, RFC 4627, only allowed an object or an array at the top of a document. Later ones allow any value, but some older parsers still reject a file that's just a string, a number, true, false, or null. Turn this rule on for files that such tools have to read.
Configure#
Off by default. It needs an ESLint config that says which files are in the language:
eslint.config.js
import json from "@eslint/json";
export default [
{
files: ["**/*.json"],
plugins: { json },
language: "json/json",
rules: { "json/top-level-interop": "error" },
},
];It also works with json/jsonc and json/json5.
Examples#
example.json1 problem
"app"Top level item should be array or object, got String.example.json1 problem
42Top level item should be array or object, got Number.example.jsonNo problems
{ "name": "app" }example.jsonNo problems
[1, 2, 3]