Remove bracket and quotations in JSON_AGG (Aggregate Functions)

Viewed 500
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,
    ]);
}

I am using JSON_AGG to retrieve multiple lines of drug_dosage and combine them into one, but I am getting a bracket and quotation in my output, how do I take it out?

1

UPDATE: I am getting errors in the examples because I am trying solutions using str_replace and preg_replace. my problem is that the target is in an SQL statement so I am suspecting that has something to do with the error since there is other data in the result Error:

  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\"]"}
2 Answers

You can try string_agg instead 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,
]);

}

Because: JSON_AGG returns JSON ARRAY as STRING. After that you returned json encoded result from controller. This adds unwanted characters for make valid json encoding. (nested quotes must be escaped).

So;

Before sending result, you must json_decode for each record's drug_dosage field.

Example code:

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,
   ]);
}

Related