andreladocruz's avatar

Collection array to Jason with Special Characters

Hi Sirs,

I'm querying the database as follows:

$results = auth()->user()->client->transactions()
            ->select('paymentType', DB::raw('COUNT(paymentType) as qty'))
            ->whereDate('confirmationDate', '>=', $baseDate)
            ->whereIn('status', ['Aprovada','Completa'])
            ->groupBy('paymentType')
            ->pluck('qty','paymentType');

The Json string returned is:

"["Boleto Banc\u00e1rio","Cart\u00e3o de Cr\u00e9dito","Paypal"]"

And should be:

"["Boleto Bancário","Cartão de Crédito","Paypal"]"

Any clue in how to fix this?

Regards,

0 likes
13 replies
iak's avatar

It's just encoded.

if you run it through json_decode it will decode properly :)

shakti's avatar

use something like this it might help you

public function respondWithJson($data)
{
    $options = app('request')->header('accept-charset') == 'utf-8' ? JSON_UNESCAPED_UNICODE : null;

    return response()->json($data, 200, $options);
}

FYI this is not the correct way to encode the json. the method which you are using is correct and while decoding the value you can encode the special chartcer

andreladocruz's avatar

Thanks for the help!!!

Let me explain it better... :)

after running the query, the data is sent to the view:

$view->with('paymentTypes', $results);

In the view I populate my vue element:

<div class="panel-body">
      <donutgraph :labels={{$paymentTypes->keys()}}
                  :quantity={{$paymentTypes->values()}}>
      </donutgraph>
    </div>

the problem is that the property labels is been filled with

:labels="["Boleto Banc\u00e1rio","Cart\u00e3o de Cr\u00e9dito","Paypal"]"

instead of

:labels="["Boleto Bancário","Cartão de Crédito","Paypal"]"

with so, all graphs in the page don't render correctly because of this error.

iak's avatar

Tried this?

{{ json_encode($paymentTypes->keys()) }}
shakti's avatar

okay. is my code working which i have provided to you

andreladocruz's avatar

As a Workaround i"m doing the following:

$keys = '';
        $values = '';
        foreach ($results as $result) {
          $keys = $keys.'"'.$result->paymentType.'",';
          $values = $values.$result->qty.",";
        }
        $view->with('keys', $keys)->with('values', $values);

and in the view:

<div class="panel-body">
  <donutgraph :labels="[{{$keys}}]"
                  :quantity="[{{$values}}]">
  </donutgraph>
</div>
shakti's avatar

in this code i m not able to find any json thing??

Can you please let me know where you are converting array into json as i m not able to fine it

andreladocruz's avatar

@shakti,

The array is converted to json dirrectly by laravel. When you try echo out an array, it converts to json.

now I can't find where it does that. :(

shakti's avatar

FYI: I think laravel convert collection section into the json through the blade.

You per your code you have just mention array.

Anyway if you are passing collection into the view then I think its hard to convert them in the view.

you can convert collection into array and then convert then in json within the controller

hope it might help you

andreladocruz's avatar

@shakti,

I have a view composer that fills the data for this specific block.

In the composer I wrote this

dd($paymentTypes->keys().'');

Note that there is a ".''" to force the cast to string.

The result was the same:

"["Boleto Banc\u00e1rio","Cart\u00e3o de Cr\u00e9dito","Paypal"]"

So, this is not a Blade issue.

Is in the casting process from array to json that laravel does.

:)

shakti's avatar
shakti
Best Answer
Level 5

IDK why its not working for you but this code work prefectly for me

$array=["Boleto Bancário","Cartão de Crédito","Paypal"];
        
echo json_encode($array,JSON_UNESCAPED_UNICODE);

you can check the screenshot https://snag.gy/YPD6Ig.jpg

1 like
usman's avatar

@andreladocruz blade escapes all the output when you echo your data using {{}} blade operators.

Now you have two options:

  1. to use {!! !!} operator and prevent the blade from escaping the output.

  2. to use JSON.parse on the props inside your component code to decode the json data back incase you're using the {{}} operator.

Your best bet is to use the 2nd option IMO because you definitely need to escape any user data to prevent security complications.

Usman.

1 like

Please or to participate in this conversation.