Skip to content

Fix 7998 json escape double quotes #3031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions enginetest/queries/json_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,44 @@ var JsonScripts = []ScriptTest{
},
},
},
{
Name: "json_object preserves escaped characters in key and values",
Assertions: []ScriptTestAssertion{
{
Query: `select cast(JSON_OBJECT('key"with"quotes\n','3"\\') as char);`,
Expected: []sql.Row{
{`{"key\"with\"quotes\n": "3\"\\"}`},
},
},
},
},
{
Name: "json conversion works with escaped characters",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT CAST(CAST(JSON_OBJECT('key"with"quotes', 1) as CHAR) as JSON);`,
Expected: []sql.Row{
{`{"key\"with\"quotes": 1}`},
},
},
},
},
{
Name: "json_object with escaped k:v pairs from table",
SetUpScript: []string{
`CREATE TABLE IF NOT EXISTS textt_7998 (t text);`,
`INSERT INTO textt_7998 VALUES ('first row\n\\'), ('second row"');`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT JSON_OBJECT(t, t) FROM textt_7998;`,
Expected: []sql.Row{
{types.MustJSON(`{"first row\n\\": "first row\n\\"}`)},
{types.MustJSON(`{"second row\"": "second row\""}`)},
},
},
},
},
{
Name: "json_value preserves types",
Assertions: []ScriptTestAssertion{
Expand Down
10 changes: 6 additions & 4 deletions sql/types/json_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ func writeMarshalledValue(writer io.Writer, val interface{}) error {

writer.Write([]byte{'{'})
for i, k := range keys {
writer.Write([]byte{'"'})
writer.Write([]byte(k))
writer.Write([]byte(`": `))
err := writeMarshalledValue(writer, val[k])
err := writeMarshalledValue(writer, k)
if err != nil {
return err
}
writer.Write([]byte(`: `))
err = writeMarshalledValue(writer, val[k])
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions sql/types/json_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ newlines
val: decimal.New(123, -2),
expected: "1.23",
},
{
name: "formatted key strings",
val: map[string]interface{}{
"baz\n\\n": "qux",
"foo\"": "bar\t",
},
expected: `{"foo\"": "bar\t", "baz\n\\n": "qux"}`,
},
}

for _, test := range tests {
Expand Down