JSON.STRLEN

Introduction and Use Case(s)

JSON.STRLEN is a Redis command used with the RedisJSON module to determine the length of a JSON string at a specified path within a JSON document. This can be particularly useful for validating data sizes, enforcing length constraints, or optimizing storage by understanding the size of JSON values.

Syntax

  1. JSON.STRLEN <key> <path>

Parameter Explanations

  • <key>: The key in Redis that holds the JSON document.
  • <path>: The JSONPath expression indicating the location of the string within the JSON document. This should point directly to a string value.

Return Values

The command returns an integer representing the length of the JSON string at the specified path.

Examples:

  • If the string’s length is 10, it returns (integer) 10.
  • If the path does not exist or does not point to a string, it returns nil.

Code Examples

  1. dragonfly> JSON.SET mydoc $ '{"name": "John", "age": 30, "city": "New York"}'
  2. OK
  3. dragonfly> JSON.STRLEN mydoc $.name
  4. (integer) 4
  5. dragonfly> JSON.STRLEN mydoc $.city
  6. (integer) 8
  7. dragonfly> JSON.STRLEN mydoc $.age
  8. (nil)

Best Practices

  • Ensure the path points directly to a string; otherwise, the command will return nil.
  • Use this command to validate input lengths when dealing with user-generated content to enforce maximum allowed sizes.

Common Mistakes

  • Providing a path that leads to a non-string element such as an object, array, or number will result in a nil return value.
  • Using incorrect JSONPath syntax might lead to unexpected results or errors.

FAQs

What happens if the provided path does not exist?

If the path does not exist or does not point to a valid string, JSON.STRLEN returns nil.

Can I use JSON.STRLEN on nested JSON strings?

Yes, you can use JSON.STRLEN on nested JSON strings by providing the correct JSONPath to the string.