Checking a top-level JSON value that is not an object
A complete JSON document does not have to be an object or an array. This worksheet formats a lone number, a lone string and an object wrapper, and shows that the parser accepts all three.
What counts as a JSON text
The first JSON specification, RFC 4627 from 2006, required a document to be an object or an array. RFC 7159, published in 2014, relaxed that rule, and RFC 8259, the current standard, keeps it: a JSON text is any serialized value. A number, a string, true, false and null are therefore each a complete document on their own. Older parsers may still reject them, which is why you sometimes see a scalar fail in one tool and pass in another. The formatter on this site follows the current standard and accepts them. The ECMA-404 standard, published in 2013 and revised in 2017, takes the same position, so the two documents agree on this point and a scalar is legal under either.
The top-level number
The first control is the single character 3. The parser accepts it as a valid document containing the number three. With an indent of 2 the output is still just 3, because indentation applies to the contents of containers, and a bare number has none. There is no brace or bracket to open, no property to place on a new line, so nothing changes. The page reports valid true and returns the token as typed. The same holds for a negative number, a decimal or an exponent form, since each is a single value token. A value with a space inside it, such as 3 4, is two values and fails.
The top-level string
The second control is "red" including its quotation marks. It is a complete document that contains one string. The quotes belong to the syntax, so removing them leaves the bare word red, which is not valid JSON. The formatted output is the same string token with its quotes. Notice that no wrapper object has been added to make it conform, since the formatter does not invent structure. The input and output differ only if the input had extra whitespace around the value. A string may hold any text, including braces and commas, so "{not an object}" is a valid scalar document that only looks like structure. Likewise the literals true, false and null are complete documents when written in lowercase, and True or None are not.
The object wrapper
The third control is {"value":3}, an object with one property whose value is 3. With an indent of 0 it formats to {"value":3}. This is a different document from the bare 3, even though it holds the same number, because it adds a container and a name. Receiving systems often prefer this form, since a named property can later be joined by other properties without breaking old readers. A bare scalar leaves no room to grow, which is one reason many interfaces use objects at the top level. Compare the two outputs: the object takes more characters, and in return it names what the 3 stands for, so a reader of the logged document knows it is a value and not an error code or a count.
Why an endpoint may still refuse a scalar
Valid JSON and acceptable input are separate questions. An API endpoint may be written to read the body as an object, look up named fields, and return an error if the document is anything else. That error is the endpoint's rule, and it does not show that your scalar is malformed. Check the interface documentation to see what top-level type is expected. If it asks for an object with named fields, wrap your value as the documentation shows. If it asks for an array, send an array even when it has one element. The HTTP status and the error text from the endpoint usually name the expected type, and reading them is faster than guessing.
Where top-level scalars appear in practice
Some uses do rely on them. A configuration system may store a single flag as true. A message queue may carry a bare string for a log line. A database column of a JSON type might hold a lone number or null for one row and an object for another. Tests often use scalars as the simplest possible documents. In each case the consumer knows the type it will see, and the format itself places no restriction. Command-line tools that print JSON often emit a bare string or number for a single result, and scripts that read it back need a parser willing to accept one.
Whitespace and what surrounds the value
Whitespace before and after a top-level value is allowed and insignificant. The only characters the standard treats as whitespace are space, horizontal tab, line feed and carriage return. A byte order mark at the start of a file is not allowed by the standard, although parsers are permitted to ignore it, so a file saved with one may fail in a strict tool. Text after the value, such as a second value, is also an error, since a document holds exactly one. Files with one JSON value per line, often called JSON Lines, are a different convention that needs a line-by-line reader, and the whole file does not pass as one document. Paste a single line at a time when you want to check one.
Scalars versus objects at the top level
The three inputs compare scalars with an object and show the parser's behavior. A passing result says nothing about whether another system takes the value, and a document with trailing text or a second value after the first would fail as a whole, since only one JSON value is allowed. If an endpoint requires an object with named fields, follow its schema and do not treat its refusal as evidence that the scalar was malformed JSON.
Choosing between a scalar, an array and an object
When you design a payload, think about how it may grow. A bare scalar is the smallest document but cannot carry a second piece of information without changing type, which forces every client to change at once. An array suits a list of similar items and leaves room for more of them. An object suits a record with named parts, and new named parts can be added later while older readers simply ignore them. For that reason most public interfaces return objects even when today's response holds a single value, and the extra braces cost only two characters.
Reproduce the checks
Top-level number
Open the Json Validator Formatter and enter:
- JSON input
3- Indent
2
Expected output:
Valid
trueFormatted
3There is no container to indent.
Top-level string
Open the Json Validator Formatter and enter:
- JSON input
"red"- Indent
2
Expected output:
Valid
trueFormatted
"red"The string token is a complete input value.
Object wrapper
Open the Json Validator Formatter and enter:
- JSON input
{"value":3}- Indent
0
Expected output:
Valid
trueFormatted
{"value":3}This form supplies a property name and changes the structure.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.
Sources
- Primary definition and convention reference ↗ · 2026-10-08