要点

以接口或模式为准;null 常表示无值/未知,"" 表示有键且值为空串,未出现键表示未传或可选。

JSON:空字符串与 null

语法上它们都有效,但语义不同:与 API 或 JSON Schema 不一致时会出现校验或业务错误。

常见原因

  • 将缺省值写成空串或 null 与模式不符。
  • 从其他格式导入时把 null 当成空串。

如何修复

  • 对照 OpenAPI/Schema 修正类型。
  • 用 JSON 校验器确认语法,再对照业务规则。

见 /json-validator-online 与 /tools。

Understanding the difference between an empty string, null, and an omitted field is essential when working with JSON APIs, form submissions, and data validation pipelines. An empty string usually means a value was provided but contains no characters, while null typically means the value is intentionally unknown, unavailable, or cleared. An omitted property means the field was not sent at all. These distinctions matter for backend validation, database updates, schema design, and client-side error handling. This page helps developers, QA teams, and API integrators identify common JSON validation issues and choose the right representation for each use case.

How This Validator Works

This JSON validation context focuses on how parsers and schemas interpret string values, null values, and missing keys. In JSON, "" is a valid string with zero length, null is a distinct literal value, and an omitted field is simply absent from the object. Validation tools, API gateways, and application code may treat these differently depending on the schema, business rules, or database constraints.

  • Empty string: a present field with no characters, such as "name": "".
  • null: an explicit null literal, such as "name": null.
  • Omitted field: the key is not included at all in the JSON object.

When a validator flags an issue, it is often because the expected type, required status, or downstream logic does not match the submitted value.

Common Validation Errors

  • Type mismatch: a field expects a string, but receives null or a number.
  • Required field missing: the property is omitted even though the schema marks it as required.
  • Empty string rejected: the schema or application logic requires a non-empty value.
  • Null not allowed: the API accepts strings only and does not permit null.
  • Incorrect update behavior: sending null clears a field, while omission leaves the existing value unchanged.
  • Schema mismatch: OpenAPI, JSON Schema, or backend validation rules disagree on whether empty values are valid.

Where This Validator Is Commonly Used

  • REST APIs and GraphQL payload handling
  • JSON Schema validation in frontend and backend systems
  • Form submission and partial update workflows
  • Database write and patch operations
  • Data import/export pipelines
  • Configuration files and environment-driven JSON settings
  • ETL, analytics, and integration testing

Why Validation Matters

Validation helps ensure that data is interpreted consistently across systems. A field that is empty, null, or missing can produce different outcomes in application logic, search indexing, reporting, and storage. For example, a profile form may allow an empty optional field, while an API update endpoint may require null to clear a value and omission to preserve it. Clear validation rules reduce ambiguity, prevent accidental data loss, and make integrations easier to maintain.

Technical Details

In JSON, string values, null values, and absent properties are not interchangeable. Their meaning depends on the schema and the consuming application.

Representation Example Typical Meaning
Empty string "email": "" A value was provided, but it contains no characters.
null "email": null The value is explicitly unknown, unavailable, or cleared.
Omitted { } with no email key The field was not sent and may retain its previous value or default.

In JSON Schema, a field may be constrained with type: "string", minLength: 1, or a nullable definition depending on the schema dialect and implementation. In API design, it is important to document whether a field is optional, nullable, or required, because clients may otherwise send values that pass parsing but fail business validation.

What is the difference between empty string and null in JSON?

An empty string is a valid string value with no characters, while null is a separate JSON literal that represents an explicit absence of value. They are not the same thing in parsing, validation, or application logic. A field can be a string, nullable, or both depending on the schema. Choosing the wrong one can change how updates, storage, and display logic behave.

When should I use an empty string?

Use an empty string when the field is meant to be a string and the value is intentionally blank, such as a user leaving an optional text input empty. This is common when the system expects a string type and you want to preserve type consistency. However, some APIs reject empty strings for required fields or fields that must contain meaningful content.

When should I use null?

Use null when you need to represent an explicit lack of value, such as clearing a previously stored field or indicating that a value is unknown. Many APIs use null to distinguish “no value” from “empty text.” Whether null is allowed depends on the schema, database column settings, and update semantics of the endpoint.

What does it mean when a JSON field is omitted?

An omitted field means the key is not present in the object at all. This is often used in partial updates, where missing fields are left unchanged. In validation, omission may be acceptable for optional fields but invalid for required fields. It is important not to assume omission and null have the same meaning, because many systems treat them differently.

Why does my API reject an empty string?

Your API may reject an empty string because the field has a minimum length requirement, a custom validator, or a backend rule that expects meaningful content. Some systems also reject empty strings for fields like IDs, emails, or phone numbers because those values must follow a specific format. Check the schema, documentation, and server-side validation rules.

Can null and empty string cause database issues?

Yes. Databases often distinguish between NULL and "", and the difference can affect uniqueness checks, indexing, filtering, and reporting. For example, a nullable column may accept null but not an empty string, or vice versa depending on application logic. Consistent handling across API, validation, and storage layers helps avoid unexpected behavior.

How do JSON Schema validators treat empty strings?

JSON Schema validators typically treat empty strings as valid string values unless a rule such as minLength: 1 is present. If the schema says the field type is string, "" usually passes type validation. If the field is required, the key must also be present. Nullable behavior depends on the schema dialect and how null is declared.

What is the safest way to document nullable fields?

Document whether a field is required, optional, nullable, or must be non-empty. Include examples for empty string, null, and omitted cases if your API supports them. Clear documentation reduces client-side mistakes and makes validation behavior easier to test. This is especially important for PATCH endpoints, form APIs, and systems that sync data across services.

How can I fix validation errors related to empty string vs null?

Start by checking the schema or API contract to see what the field expects. If the field requires a non-empty string, send a valid string instead of "" or null. If the field is nullable, use null only when the API explicitly allows it. For partial updates, confirm whether omission preserves the existing value and whether null clears it.

Related Validators & Checkers

  • JSON Validator
  • JSON Schema Validator
  • API Payload Validator
  • XML Validator
  • Structured Data Validator
  • Data Type Validator
  • Syntax Checker

常见问题

空字符串是合法 JSON 吗?
是。它是空字符串,不同于 null 或缺键。
何时用 null?
在约定中需要显式“无/未知”且类型为 null 时。

相关

所有工具 · 规范地址