/
Da

What makes a good JSON? Which rules to follow for my data?

A comprehensive guide to selecting, formatting, and understanding JSON keys for creating well-organized data structures while ensuring compatibility with parsers.

What makes a good JSON? Which rules to follow for my data?

Introduction

In JSON (JavaScript Object Notation), keys are strings used to identify the values within an object. These keys act as identifiers in key-value pairs and play a vital role in structuring data. To ensure data integrity and compatibility across various systems, JSON keys must follow a set of syntactic rules and character constraints.

Allowed Characters in JSON Keys

According to the JSON specification, the characters allowed in JSON keys include:

  1. Letters (both lowercase and uppercase): a-z, A-Z
  2. Digits: 0-9
  3. Underscore: _
  4. Hyphen: -

While these are the most commonly accepted characters, it's worth noting that since JSON keys are simply strings, virtually any Unicode character can be used. However, sticking to the characters above ensures better compatibility and fewer issues with parsers or programming languages that consume JSON.

JSON Key Rules

In addition to the character set, there are several important rules to remember when defining JSON keys:

  • Keys must be enclosed in double quotation marks (" "). Example: "name": "Alice"
  • Keys are case-sensitive, so "key" and "Key" are treated as distinct. Example:
{
  "key": "value1",
  "Key": "value2"
}


  • Keys should not start with a digit, though technically JSON allows it. However, starting with a letter or underscore is a best practice for readability and compatibility.

Valid JSON Key Examples

Here are some examples of valid JSON keys:

{
  "username": "johndoe",
  "user_id": 123,
  "user-name": "johnny",
  "_internalFlag": true,
  "email1": "john@example.com"
}

All keys above follow the accepted conventions and will parse correctly in any standard JSON parser.

Best Practices for Naming JSON Keys

While the JSON format is relatively permissive, using clear and consistent naming conventions will make your data more maintainable and readable. Here are some tips:

  • Use camelCase or snake_case consistently throughout your data.
  • Avoid using spaces or special characters in keys (even if technically allowed).
  • Make keys descriptive, but not overly verbose.
  • Reserve short or abbreviated keys (like id, uid) for widely understood identifiers.

Conclusion

JSON keys are essential to organizing and accessing structured data effectively. Although the rules are simple, adhering to best practices in naming and character usage will go a long way in ensuring that your JSON data remains clean, consistent, and interoperable across platforms.

When in doubt, keep it simple: stick to letters, numbers, underscores, and hyphens, and always wrap your keys in double quotes.