D
D
Damir Shaniyazov2021-05-25 09:36:15
Laravel
Damir Shaniyazov, 2021-05-25 09:36:15

How to pass boolean to json instead of int?

How to pass boolean to json instead of int?
First, I load the data from the database:

$rows = DB::select($sql);

Next, I process the array, push the necessary rows into the array, which will be sent as json:

if ($row['pid'] == $request->get('id')) {
                    $arr = array(
                        'id' => $row['id'],
                        'parent' => $row['pid'],
                        'text' => $row['shortname'],
                        'children' => true,
                    );
                    array_push($json, $arr);
                }

Posting:

return response()->json($json);

So children contains 1\0 instead of true\false .

Tried it like this:

return response(json_encode($json), 200)->header('Content-Type', 'application/json');

But I am getting an error:

json_encode(): Invalid UTF-8 sequence in argument

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question