diff --git a/be/src/exprs/json_functions.cpp b/be/src/exprs/json_functions.cpp index 88c238d482..470902f04d 100644 --- a/be/src/exprs/json_functions.cpp +++ b/be/src/exprs/json_functions.cpp @@ -281,6 +281,16 @@ rapidjson::Value* JsonFunctions::get_json_array_from_parsed_json( return nullptr; } + if (parsed_paths.size() == 1) { + // the json path is "$", just return entire document + // wrapper an array + rapidjson::Value* array_obj = nullptr; + array_obj = static_cast(mem_allocator.Malloc(sizeof(rapidjson::Value))); + array_obj->SetArray(); + array_obj->PushBack(*document, mem_allocator); + return array_obj; + } + rapidjson::Value* root = match_value(parsed_paths, document, mem_allocator, true); if (root == nullptr || root == document) { // not found return nullptr; @@ -301,6 +311,11 @@ rapidjson::Value* JsonFunctions::get_json_object_from_parsed_json( return nullptr; } + if (parsed_paths.size() == 1) { + // the json path is "$", just return entire document + return document; + } + rapidjson::Value* root = match_value(parsed_paths, document, mem_allocator, true); if (root == nullptr || root == document) { // not found return nullptr; diff --git a/be/test/exprs/json_function_test.cpp b/be/test/exprs/json_function_test.cpp index 0f7693631a..d1c4a4fbf7 100644 --- a/be/test/exprs/json_function_test.cpp +++ b/be/test/exprs/json_function_test.cpp @@ -187,8 +187,8 @@ TEST_F(JsonFunctionTest, special_char) { TEST_F(JsonFunctionTest, json_path1) { std::string json_raw_data( - "[{\"k1\":\"v1\", \"keyname\":{\"ip\":\"10.10.0.1\", \"value\":20}},{\"k1\":\"v1-1\", " - "\"keyname\":{\"ip\":\"10.20.10.1\", \"value\":20}}]"); + "[{\"k1\":\"v1\",\"keyname\":{\"ip\":\"10.10.0.1\",\"value\":20}},{\"k1\":\"v1-1\"," + "\"keyname\":{\"ip\":\"10.20.10.1\",\"value\":20}}]"); rapidjson::Document jsonDoc; if (jsonDoc.Parse(json_raw_data.c_str()).HasParseError()) { ASSERT_TRUE(false); @@ -207,6 +207,14 @@ TEST_F(JsonFunctionTest, json_path1) { for (int i = 0; i < res3->Size(); i++) { std::cout << (*res3)[i].GetString() << std::endl; } + + res3 = JsonFunctions::get_json_array_from_parsed_json("$", &jsonDoc, jsonDoc.GetAllocator()); + ASSERT_TRUE(res3->IsArray()); + rapidjson::StringBuffer buffer; + buffer.Clear(); + rapidjson::Writer writer(buffer); + (*res3)[0].Accept(writer); + ASSERT_EQ(json_raw_data, std::string(buffer.GetString())); } TEST_F(JsonFunctionTest, json_path_get_nullobject) {