go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

JSON-Set : Not able to update fields with string values

Open Prophet0fRegret opened this issue 10 months ago • 1 comments

JSON-Set command is not able to update the specified field in the command with the supplied value if it is a STRING. It completes successfully if the value is something other, for example, an integer.

Expected Behavior

JSON-Set should be able to update JSON fields with string values.

Current Behavior

JSON-Set is not able to update JSON fields with string values. The command returns the following error : "expected value at line 1 column 1"

Possible Solution

Error is returned when entering the following command in CLI or from the lib : JSON:SET <key> <path.subpath> <STRING value to be set>

But no errors are returned when the same operation is tried using the following command from the CLI : JSON:SET <key> <path.subpath> '"<STRING value to be set>"'

The issue gets fixed if you enclose the string value with double-quotes (""), & then also enclose it within single-quotes('). Does not work if you try either of them, or none.

Steps to Reproduce

For the record, I believe I am on the latest version of the package, which is v9.5.1, at the time of reporting this bug.

  1. Try to set a new JSON object using a similar command : JSON.SET doc . '{"a":"2"}'
  2. Try to update the key, here, a, with a similar command : JSON.SET doc .a "7"
  3. Receive the above mentioned error

Context (Environment)

I am trying to update a field in an existing JSON document in REDIS. Using the go-redis package, I am able to set a new JSON object with the JsonSET() function. But when trying to utilise the same function to update a specific field with a string value, it gives the error.

I am not able to find any other alternative function which allows to update the fields. When faced with the same issue on the CLI, I tried different combinations and deduced that the string value that I am trying to update for the field needs to be enclosed in both th double-quotes and single-quotes for it to work.

Possible Implementation

I believe the change should be in the way the string is being parsed and sent to REDIS. I tried some stuff, like adding escape characters, but it still gives the same issue.

Prophet0fRegret avatar Apr 08 '24 12:04 Prophet0fRegret

Any Reproducable code for the issue would be helpful.

My code below cannot reproduce:

package main

import (
	"context"
	"github.com/redis/go-redis/v9"
)

func main() {
	ctx := context.Background()

	rdb := redis.NewClient(&redis.Options{})

	if err := rdb.Ping(ctx).Err(); err != nil {
		panic(err)
	}

	key := "doc"

	if err := rdb.JSONSet(ctx, key, ".", "{\"a\": \"2\"}").Err(); err != nil {
		panic(err)
	}

	if err := rdb.JSONSet(ctx, key, ".a", "7").Err(); err != nil {
		panic(err)
	}

	if err := rdb.Del(ctx, key).Err(); err != nil {
		panic(err)
	}
}

tzq0301 avatar Jul 13 '24 13:07 tzq0301