Ibzy's avatar
Level 2

Query through JSON file

Is it possible to query through my JSON file without using a database. When i return my JSON object I am only able to get it in raw. If I try to access an element in my array I get Trying to get property 'name' of non-object.

This is my method in the route for testing.

Route::get('/test', function(){
    $jsonString = file_get_contents(base_path('database/db.json'));

    $data = json_decode($jsonString, true);

    collect($data);

    return $data->name;

});

How can I make differet queries like I would do with my database with where ?

0 likes
5 replies
Cronix's avatar

You'd have to show what $data looks like after you decode it.

1 like
Ibzy's avatar
Level 2

@CRONIX -

[
{
    "name": "Joe John",
    "city": "London",
"sports": [
"Football",
"Rugby",
"Basketball"
],
"description": "Voluptatem qui eum eius aut."
},
{
"name": "Josh Round",
"city": "Madrid",
"sports": [
"Rugby",
"Cricket",
"Baseball"
],
"description": "Natus in repellat voluptate vitae."
},

Cronix's avatar

There are multiple objects with a name property within an array. It's not a single object that contains a name property, so return $data->name; wouldn't work. which name are you trying to return?

Ibzy's avatar
Level 2

@CRONIX - I am trying to create a controller where i can search through this data for example if i search for the sport rugby it returns all the people who play rugby

jlrdw's avatar

Look at this example: https://stackoverflow.com/questions/8177993/how-can-i-get-value-through-json-array-like-query

Once you, with trial and error, figure the correct loop to loop over the data:

  • In the loop if Rugby is there, then
  • put that whole row into a new temp array
  • once finished the looping of first array
  • send second temp array to view
  • now foreach using blade the array containing your Rugby

OR

As looping original json array, write to a temp table then query the table, then send results to view.

And or

Write a custom class that does this stuff.

Sorry no cut and paste code, but really a lot of what you need is just some trial and error, tweaks to get correct. Some leg work in other words.

Not a solution, but a starting point:

    public function rug()
    {
        $data = '[
        {
        "name": "Joe John",
        "city": "London",
        "sports": [
        "Football",
        "Rugby",
        "Basketball"
        ],
        "description": "Voluptatem qui eum eius aut."
        }, {
            "name": "Josh Round",
            "city": "Madrid",
            "sports": [
            "Rugby",
            "Cricket",
            "Baseball"
            ],
            "description": "Natus in repellat voluptate vitae."
        }]';
        $quy = json_decode($data, true);
        echo '<pre>';
        print_r($quy);
        echo '</pre>';

        echo "<br>";
        $keys = array_keys($quy);
        //dd($loads);
        for ($i = 0; $i < count($quy); $i++) {
            foreach ($quy[$i] as $key => $value) {  //$key => $value not used here
                
                
                if (in_array('Rugby', $quy[$i]['sports'])) {
                    echo "it is there";
                    // So work out a routine to get all of that $i
                    // and put into a another temp array
                    // to eventually pass to a blade view.
                }
            }
        }
       
    }

Output:

Array
(
    [0] => Array
        (
            [name] => Joe John
            [city] => London
            [sports] => Array
                (
                    [0] => Football
                    [1] => Rugby
                    [2] => Basketball
                )

            [description] => Voluptatem qui eum eius aut.
        )

    [1] => Array
        (
            [name] => Josh Round
            [city] => Madrid
            [sports] => Array
                (
                    [0] => Rugby
                    [1] => Cricket
                    [2] => Baseball
                )

            [description] => Natus in repellat voluptate vitae.
        )

)


it is thereit is there

Inserting this stuff into a table so you can query it would be easier in my opinion.

1 like

Please or to participate in this conversation.