Establishing Consistency in JSON Structure for API Interactions

Create standards for structuring JSON data in API interactions to reduce errors and enhance data handling.

0 likes
45 views

Rule Content

{
  "title": "Establishing Consistency in JSON Structure for API Interactions",
  "description": "Create standards for structuring JSON data in API interactions to reduce errors and enhance data handling.",
  "category": "JavaScript Cursor Rules",
  "rules": [
    {
      "name": "Use Consistent Key Naming Conventions",
      "description": "Ensure uniformity in JSON key names to maintain readability and avoid conflicts.",
      "recommendation": "Adopt camelCase for JSON property names, aligning with JavaScript conventions.",
      "example": {
        "good": {
          "firstName": "John",
          "lastName": "Doe"
        },
        "bad": {
          "first_name": "John",
          "last_name": "Doe"
        }
      }
    },
    {
      "name": "Proper Indentation and Spacing",
      "description": "Format JSON data with consistent indentation to enhance readability.",
      "recommendation": "Use 2 spaces for indentation in JSON files.",
      "example": {
        "good": {
          "user": {
            "id": 123,
            "name": "Alice",
            "roles": ["admin", "editor"]
          }
        },
        "bad": {
          "user": {
            "id": 123,
            "name": "Alice",
            "roles": ["admin", "editor"]
          }
        }
      }
    },
    {
      "name": "Avoid Trailing Commas",
      "description": "Prevent parsing errors by ensuring no trailing commas are present in JSON structures.",
      "recommendation": "Remove any trailing commas in JSON objects and arrays.",
      "example": {
        "good": {
          "name": "Alice",
          "age": 25
        },
        "bad": {
          "name": "Alice",
          "age": 25,
        }
      }
    },
    {
      "name": "Use Arrays for Collections",
      "description": "Represent collections of related data using arrays to maintain a clear structure.",
      "recommendation": "Group related items within arrays instead of using multiple separate keys.",
      "example": {
        "good": {
          "employees": [
            {"name": "John", "role": "Developer"},
            {"name": "Jane", "role": "Designer"}
          ]
        },
        "bad": {
          "employee1": {"name": "John", "role": "Developer"},
          "employee2": {"name": "Jane", "role": "Designer"}
        }
      }
    },
    {
      "name": "Validate JSON Regularly",
      "description": "Ensure JSON data is correctly formatted and free of errors before deployment.",
      "recommendation": "Use JSON validation tools to detect syntax errors.",
      "example": {
        "tools": ["JSONLint", "Online JSON Formatter", "Postman"]
      }
    },
    {
      "name": "Escape Special Characters Properly",
      "description": "Prevent errors by correctly escaping special characters in JSON strings.",
      "recommendation": "Use backslashes to escape special characters within strings.",
      "example": {
        "good": {
          "message": "Hello \"world\"!"
        },
        "bad": {
          "message": "Hello "world"!"
        }
      }
    },
    {
      "name": "Format Date and Time Fields Using ISO 8601",
      "description": "Standardize date and time representations to ensure consistency across systems.",
      "recommendation": "Use the ISO 8601 format for all date and time fields.",
      "example": {
        "good": {
          "createdAt": "2025-06-09T14:42:43Z"
        },
        "bad": {
          "createdAt": "06/09/2025 2:42 PM"
        }
      }
    },
    {
      "name": "Avoid Using Null Values",
      "description": "Prevent ambiguity and potential errors by not using null values in JSON data.",
      "recommendation": "Omit properties with null values or use empty strings/arrays as appropriate.",
      "example": {
        "good": {
          "name": "Alice",
          "email": ""
        },
        "bad": {
          "name": "Alice",
          "email": null
        }
      }
    }
  ]
}