删除 JSON_AGG 中的括号和引号(聚合函数)
Remove bracket and quotations in JSON_AGG (Aggregate Functions)
public function fetchdrug(Request $search_drug){
$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
JSON_AGG(drug_dosage) AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();
return response()->json([
'all_drugs'=>$all_drugs,
]);
}
我正在使用 JSON_AGG
检索多行 drug_dosage
并将它们合并为一行,但我在输出中得到一个括号和引号,我该如何将其取出?
更新:
我在示例中遇到错误,因为我正在尝试使用 str_replace
和 preg_replace
的解决方案。我的问题是目标在 SQL 语句中,所以我怀疑这与错误有关,因为结果中还有其他数据
错误:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in
{"drug_code":"CFZU",
"drug_name":"Cefazolin",
"dosage_list":"[\"<=4 mg\/L\", \"<=3 mg\/L\"]"},
{"drug_code":"TZPD","drug_name":"Pip\/Tazobactam",
"dosage_list":"[\"Pip\/Tazobactam\"]"}
您可以尝试 string_agg
而不是 JSON_AGG
public function fetchdrug(Request $search_drug){
$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
string_agg(drug_dosage, ', ') AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();
return response()->json([
'all_drugs'=>$all_drugs,
]);
}
因为:JSON_AGG returns JSON ARRAY
作为 STRING。之后,您从控制器返回 json 编码结果 。这会为 make valid json encoding 添加不需要的字符。 (必须转义嵌套引号)。
所以;
在发送结果之前,您必须 json_decode 每条记录的 drug_dosage 字段。
示例代码:
public function fetchdrug(Request $search_drug){
$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
string_agg(drug_dosage, ', ') AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();
foreach($all_drugs as $drug){
//decode postgresql 'json array like string presentation' to array.
$decoded = json_decode($drug->drug_dosage);
// if you want to remove null/empty values use array_filter
$filtered = array_filter($decoded); // default behavior removes falsy values.
// use same field to hold wanted, structured values
$drug->drug_dosage = $filtered;
}
// And return as json response like before.
return response()->json([
'all_drugs'=>$all_drugs,
]);
}
public function fetchdrug(Request $search_drug){
$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
JSON_AGG(drug_dosage) AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();
return response()->json([
'all_drugs'=>$all_drugs,
]);
}
我正在使用 JSON_AGG
检索多行 drug_dosage
并将它们合并为一行,但我在输出中得到一个括号和引号,我该如何将其取出?
更新:
我在示例中遇到错误,因为我正在尝试使用 str_replace
和 preg_replace
的解决方案。我的问题是目标在 SQL 语句中,所以我怀疑这与错误有关,因为结果中还有其他数据
错误:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in
{"drug_code":"CFZU",
"drug_name":"Cefazolin",
"dosage_list":"[\"<=4 mg\/L\", \"<=3 mg\/L\"]"},
{"drug_code":"TZPD","drug_name":"Pip\/Tazobactam",
"dosage_list":"[\"Pip\/Tazobactam\"]"}
您可以尝试 string_agg
而不是 JSON_AGG
public function fetchdrug(Request $search_drug){
$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
string_agg(drug_dosage, ', ') AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();
return response()->json([
'all_drugs'=>$all_drugs,
]);
}
因为:JSON_AGG returns JSON ARRAY
作为 STRING。之后,您从控制器返回 json 编码结果 。这会为 make valid json encoding 添加不需要的字符。 (必须转义嵌套引号)。
所以;
在发送结果之前,您必须 json_decode 每条记录的 drug_dosage 字段。
示例代码:
public function fetchdrug(Request $search_drug){
$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
string_agg(drug_dosage, ', ') AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();
foreach($all_drugs as $drug){
//decode postgresql 'json array like string presentation' to array.
$decoded = json_decode($drug->drug_dosage);
// if you want to remove null/empty values use array_filter
$filtered = array_filter($decoded); // default behavior removes falsy values.
// use same field to hold wanted, structured values
$drug->drug_dosage = $filtered;
}
// And return as json response like before.
return response()->json([
'all_drugs'=>$all_drugs,
]);
}