0

I have multidimensional array in PHP which I have converted into json using json encode. Now when I do console.log in javascript it shows undefined.

When I do JSON.parse the error shows

jquery.js:687 Uncaught SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)

This is my json encoded data

"{"select_data":[{"id":10563,"fullname":"Raj Bahadur Thapa","created_by":121,"company_id":68,"company_name":"WhiteHat","selected_at":"2019-10-03 12:14:18","t_count":1,"p_count":3.571428571428571},{"id":10554,"fullname":"Sebastian Antony Sahayan","created_by":121,"company_id":19,"company_name":"Concentrix","selected_at":"2019-10-03 11:22:15","t_count":3,"p_count":10.714285714285714},{"id":10545,"fullname":"Natasha Fernandes","created_by":121,"company_id":21,"company_name":"Firstsource","selected_at":"2019-10-03 10:37:59","t_count":3,"p_count":10.714285714285714},{"id":10537,"fullname":"Farahnaaz Khan","created_by":121,"company_id":90,"company_name":"Hexaware","selected_at":"2019-10-03 10:08:17","t_count":2,"p_count":7.142857142857142},{"id":9788,"fullname":"Saneesh M","created_by":121,"company_id":11,"company_name":"Tele Performance ","selected_at":"2019-10-03 09:58:38","t_count":19,"p_count":67.85714285714286}],"p_count_arr":[3.571428571428571,10.714285714285714,10.714285714285714,7.142857142857142,67.85714285714286]}"

Laravel QUERY

public function company_wise_select()
    {

        $sub_query = DB::table('in_lineups_tracker')
            ->join('in_lineups', 'in_lineups.id', '=', 'in_lineups_tracker.candidate_id')
            ->join('companies', 'companies.id', '=', 'in_lineups.company_id')
            ->join('users', 'users.id', '=', 'in_lineups.created_by')
            ->where([
                ['in_lineups_tracker.status', 19],
                ['in_lineups_tracker.type', 3],
                ['users.id', 405]
            ])
            ->groupBy('in_lineups.id')
            ->select(
                'in_lineups.id',
                'in_lineups.fullname',
                'in_lineups.created_by',
                'companies.id as company_id',
                'companies.name as company_name',
                'in_lineups_tracker.created_at as selected_at'
            );

        $el_query = DB::table(DB::raw("({$sub_query->toSql()}) as table_001"))
            ->mergeBindings($sub_query)
            ->select(
                '*',
                DB::raw('COUNT(*) as t_count')
            )
            ->groupBy('table_001.company_id')
            ->orderBy('table_001.selected_at', 'DESC')
            ->limit(5)
            ->get();


        $totalCount = $el_query->sum('t_count');

        foreach ($el_query as $select)
        {
            $select->p_count = ($select->t_count/$totalCount) * 100;
            $p_count_arr[] = $select->p_count;
        }

        $data['select_data'] = $el_query;
        $data['p_count_arr'] = $p_count_arr;
        return json_encode($data);

    }

How can I json decode it in javascript and utilize the data?

7
  • 1
    Can you show that php code? Commented Oct 16, 2019 at 5:43
  • Your JSON is incorrect, Try JSON VALIDATE Commented Oct 16, 2019 at 5:47
  • It's undefined? Error seems to be questioning the validity of JSON instead. Commented Oct 16, 2019 at 5:50
  • The value you posted as "json encoded data" is neither JSON, nor a JavaScript string. How do you transfer the data from the PHP script to the JavaScript code? Do you use Ajax or the PHP code generates the JavaScript code? Commented Oct 16, 2019 at 6:14
  • I am json encoding Laravel collection Commented Oct 16, 2019 at 13:38

2 Answers 2

2

Try this out

<script>
var data= <?= json_encode($array) ?>
console.log(data);
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I don't want to go by this method. I am ecoding my data backend side only but javascript is says data is undefined
-1

You can use JSON.stringify() to acheive this

var JsonObj = <?php echo $json;?>;
var myJsonData = JSON.stringify(JsonObj);

console.log(myJsonData);

2 Comments

No they can't. See the first sentence of the question which says the raw data is in PHP, not JavaScript.
You can replace your PHP variable by adding <?php echo $json; ?> in JsonObj

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.