diff --git a/mysql-test/r/func_json.result b/mysql-test/r/func_json.result index ac604b9e09c..ae20b8c6849 100644 --- a/mysql-test/r/func_json.result +++ b/mysql-test/r/func_json.result @@ -138,6 +138,9 @@ json_extract('[10, 20, [30, 40]]', '$[2][*]') select json_extract('[10, 20, [{"a":3}, 30, 40]]', '$[2][*]'); json_extract('[10, 20, [{"a":3}, 30, 40]]', '$[2][*]') [{"a":3}, 30, 40] +select json_extract('1', '$'); +json_extract('1', '$') +1 select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.k1', 'word'); json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.k1', 'word') {"a":1, "b":{"c":1, "k1":"word"}, "d":[1, 2]} diff --git a/mysql-test/t/func_json.test b/mysql-test/t/func_json.test index 2b4385a43b9..2b0f1cb662e 100644 --- a/mysql-test/t/func_json.test +++ b/mysql-test/t/func_json.test @@ -54,6 +54,7 @@ select json_extract('{"key0":true, "key1":"qwe"}', "$.key1"); select json_extract(json_object('foo', 'foobar'),'$'); select json_extract('[10, 20, [30, 40]]', '$[2][*]'); select json_extract('[10, 20, [{"a":3}, 30, 40]]', '$[2][*]'); +select json_extract('1', '$'); select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.k1', 'word'); select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.d[3]', 3); diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index 829a547bb07..9978d86e97d 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -476,9 +476,6 @@ String *Item_func_json_extract::val_str(String *str) v_len= je.s.c_str - value; } - if (json_scan_next(&je) && je.s.error) - goto error; - if (!multiple_values_found) { if (first_value == NULL) @@ -489,7 +486,6 @@ String *Item_func_json_extract::val_str(String *str) */ first_value= (const char *) value; first_len= v_len; - continue; } else { @@ -500,15 +496,20 @@ String *Item_func_json_extract::val_str(String *str) } } - if (str->append(", ", 2) || - str->append((const char *) value, v_len)) + if (multiple_values_found && + (str->append(", ", 2) || + str->append((const char *) value, v_len))) goto error; /* Out of memory. */ - } - if (je.s.error) - goto error; + if (json_scan_next(&je)) + break; + + } } + if (je.s.error) + goto error; + if (first_value == NULL) { /* Nothing was found. */